Macro for Word to PDF save pages

jevi

Active Member
Joined
Apr 13, 2010
Messages
339
Office Version
  1. 2016
Platform
  1. Windows
Hi All,
I have a word document with 200 pages and I need each page of the word document to be saved in PDF. For the rename of the document in PDF it would be great to have the name which is written in second row of each page of word. The first row has: Dear Madaman/Sir and the second row has the Name and Surname which is great to rename the PDF document.

Thank you so much,
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
I found it on the internet and it worked perfectly to save multiple pages from word to PDF with the name written in the second row.
thnx

VBA Code:
Sub SaveAsSeparatePDFs()

    Dim I As Long
    Dim xDlg As FileDialog
    Dim xFolder As Variant
    Dim FileName As String
    Dim myPath As String
    Dim xStart, xEnd As Integer
    Dim rowNumber
    'On Error GoTo lbl
    
    'Set xDlg = Application.FileDialog(msoFileDialogFolderPicker)
    'If xDlg.Show <> -1 Then Exit Sub
    'xFolder = xDlg.SelectedItems(1)
    xFolder = ActiveDocument.Path & "\"
    myPath = ActiveDocument.FullName
    FileName = Mid(myPath, InStrRev(myPath, "\") + 1, InStrRev(myPath, ".") - InStrRev(myPath, "\") - 1) & "_"
    
    xStart = CInt(InputBox("Start Page", "KuTools for Word"))
    xEnd = CInt(InputBox("End Page:", "KuTools for Word"))
    rowNumber = 8
    
    If xStart <= xEnd Then
        For I = xStart To xEnd
        
            Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, count:=I
            Selection.GoTo What:=wdGoToLine, Which:=wdGoToRelative, count:=rowNumber
            Selection.HomeKey Unit:=wdLine
            Selection.EndKey Unit:=wdLine, Extend:=wdExtend
            s1 = Replace(Trim(Selection), ".", "")
            s1 = Replace(s1, " ", " ")
            s1 = Replace(s1, vbCr, " ")

            s2 = xFolder & "f_" & I & "_" & s1 & "ISEE 2020.pdf"
            ' MsgBox (s2)
        
            ActiveDocument.ExportAsFixedFormat OutputFileName:= _
                s2, ExportFormat:=wdExportFormatPDF, _
                OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, Range:= _
                wdExportFromTo, From:=I, To:=I, Item:=wdExportDocumentContent, _
                IncludeDocProps:=False, KeepIRM:=False, CreateBookmarks:= _
                wdExportCreateHeadingBookmarks, DocStructureTags:=True, _
                BitmapMissingFonts:=False, UseISO19005_1:=False
        Next
    End If
    MsgBox ("Produzione pdf completata")
    Exit Sub
lbl:
    MsgBox "Enter right page number", vbInformation, "KuTools for Word"
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,980
Messages
6,175,764
Members
452,668
Latest member
mrider123

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top