Consulting

Results 1 to 4 of 4

Thread: Word Macro for saving PDF files according to the text inside of the file

  1. #1

    Word Macro for saving PDF files according to the text inside of the file

    Hi Guys, I have a WORD file which includes 100 job offers for employees. Every 2 page in the document belongs to 1 person so I wrote the following code to do so and save as PDF separately. like:
    PAGE1.2 = PDF1
    PAGE3.4 = PDF2
    and so on..

    The problem is renaming the files. If you check in the code where I specified "FirstPara" - That's where the naming the file is happening. Right now it will take the 3rd line of the first document only! it will not go to the next loop. I don't know how to connect it to "i" so it would iterate every time and select the 3rd line of next page.
    Basically, it's a nested loop I think?

    PLEASE HELPPPP!

    
    Sub SaveAsSeparatePDFs()
    
    
    Dim strDirectory As String, strTemp As String, ipgEnd As Integer
    Dim iPDFnum As Integer, i As Integer
    
    
      
    1:
    strTemp = InputBox("How many pages is included?" & vbNewLine & "(ex: 60)")
    ipgEnd = CInt(strTemp)
    strDirectory = Environ("USERPROFILE") & "\Desktop"
            
    
    
    iPDFnum = 1
    
    
    For i = 1 To ipgEnd
    
    
            Set r = ActiveDocument.Range
            r.End = r.End - 1
            FirstPara = r.Paragraphs(1).Range.Text
            FirstPara = Left(FirstPara, Len(FirstPara) - 1)
        
        ActiveDocument.ExportAsFixedFormat OutputFileName:= _
            strDirectory & "\JobOffer--" & iPDFnum & FirstPara & ".pdf", ExportFormat:=wdExportFormatPDF, _
            OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, Range:= _
            wdExportFromTo, From:=i, To:=i + 1, Item:=wdExportDocumentContent, _
            IncludeDocProps:=False, KeepIRM:=False, CreateBookmarks:= _
            wdExportCreateHeadingBookmarks, DocStructureTags:=True, _
            BitmapMissingFonts:=False, UseISO19005_1:=False
        iPDFnum = iPDFnum + 1
        
        i = i + 1
    
    
    
    
    Next i
    
    
    End
    
    
    End Sub

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Since this is most likely the result of a mailmerge, see Split Merged Output to Separate Documents or, better still, Send Mailmerge Output to Individual Files in: Mailmerge Tips & Tricks (msofficeforums.com)
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    The same question was also cross-posted at StackOverflow, but was deleted after it was observed there that the required edits to the code were trivial...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •