(Solved) – Storing parahraphs (strings) into an array which will be used to iterate – word vba


I’ve been modifying a macro I found on youtube to separate pages (mailing) into new pdf files. I added some code to add into the file name the name of the company. However when I run the code it says “It is not a valid file name.”
The issue is when I store the pharagraphs into company(i). I debug.print it and I see something like this ” Text here “. How can I solve this?

Sub test()
Dim i, j, k, l As Long 'Iteradores
Dim var As Variant 'Parrafo
Dim org As Variant 'Condición 1
Dim org2 As Variant 'Condición 2
Dim company(100) As Variant 'Array
Dim num_paginas As Integer
Dim num_doc As Integer
Dim pag_inicial As Integer
Dim pagina_final As Integer
Dim URL As String
Dim nombres As String

j = 1
org = ActiveDocument.Paragraphs(2).Range.Text
org2 = ActiveDocument.Paragraphs(20).Range.Text
For k = 1 To ActiveDocument.Paragraphs.Count
    var = ActiveDocument.Paragraphs(k).Range.Text
        If var = org Or var = org2 Then
        company(j) = Trim((ActiveDocument.Paragraphs(k   4).Range.Text))
        j = j   1
        End If
Next k

For l = 1 To 100
Debug.Print VarType(company(l))
Next l

num_paginas = InputBox("Ingrese el numero de paginas por documento")
num_doc = InputBox("¿Cuantos documentos desea generar?")
URL = InputBox("¿Donde desea crear los documentos?")
nombres = InputBox("¿Que nombre tendran los Documentos?")
pag_inicial = 1
pagina_final = num_paginas

MsgBox (URL & "" & "0" & i & " - " & nombres & company(1) & ".pdf") 'Test

For i = 1 To num_doc
    ActiveDocument.ExportAsFixedFormat OutputFileName:= _
        URL & "" & "0" & i & " - " & nombres & company(i) & ".pdf", ExportFormat:= _
        wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
        wdExportOptimizeForPrint, Range:=wdExportFromTo, From:=pag_inicial, To:=pagina_final, Item:= _
        wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
        CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
        BitmapMissingFonts:=True, UseISO19005_1:=False
    ChangeFileOpenDirectory URL

pag_inicial = pagina_final   1
pagina_final = pagina_final   num_paginas
Next i
End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *