PDA

View Full Version : Word document how to save 2 pages in 1 pdf file in a folder



wdg1
07-01-2020, 01:47 AM
If I want to print the current page in a WORD document, ad PDF file and save it to the folder c:\folder\, the macro that works fine is easy to program (see under).
Now I want to print 2 pages, the page where my cursor is in the word document + the following page.
So these two pages have to be saved as pdf file.
How do I do this?
Thanks. (Edward).

Sub print_this_page_to_pdf_in_folder()
Dim sName As String
Dim sPath As String
With ActiveDocument
sName = Left(.Name, InStr(.Name, ".") - 1)
sName = sName & ".pdf"
sPath = "C:\folder"

.ExportAsFixedFormat _
OutputFileName:=sPath & sName, _
ExportFormat:=wdExportFormatPDF, _
Range:=wdPrintCurrentPage
End With
End Sub

gmayor
07-01-2020, 05:45 AM
The following, based on your original parameters will do that


Dim sName As String
Dim sPath As String
Dim i As Long
i = Selection.Information(wdActiveEndPageNumber)
With ActiveDocument
sName = Left(.Name, InStr(.Name, ".") - 1)
sName = sName & ".pdf"
sPath = "C:\folder\"
MsgBox sPath & sName
ActiveDocument.ExportAsFixedFormat OutputFileName:=sPath & sName, _
ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=False, _
OptimizeFor:=wdExportOptimizeForPrint, _
Range:=wdExportFromTo, From:=i, To:=i + 1

End With

wdg1
07-01-2020, 06:46 AM
Works great! This rocks (again).
Thanks.
.
To finetune the macro:
.
Can the PDF file be named before saving?
.

gmayor
07-02-2020, 12:30 AM
The macro creates a PDF named from the document (as in your original), so it would have to be saved first. The alternative is to supply the macro with a name for the PDF.