VBA to save Word files as PDF

kccosavannah

Board Regular
Joined
Sep 30, 2014
Messages
82
I have a word document with about 350 pages total. I would like to print each page as a separate PDF. I would like to save each file and reference the quote number that is on each sheet. So page 1 would be 154789.pdf and page two might be 1578964.pdf.

I have the following code that is working but it is saving each page but it is asking me to save each page and creating a separate word doc and then saving the PDF.

Code:
Sub savepdf()
Dim Counter As Long, Source As Document, Target As Document
    Set Source = ActiveDocument
    Selection.HomeKey Unit:=wdStory
    Pages = Source.BuiltInDocumentProperties(wdPropertyPages)
    Counter = 0
    While Counter < Pages
    Counter = Counter + 1
    DocName = "Page" & Format(Counter) & ".pdf"
    Source.Bookmarks("\Page").Range.Cut
    Set Target = Documents.Add
    Target.Range.Paste
    Target.SaveAs FileName:=DocName, FileFormat:=wdFormatPDF
    Target.Close
Wend
End Sub

Can someone help me amend this code?
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I had difficult understanding your issues with all the "buts" - but from testing the code, I think the issue is the prompt when you close the new document. If so, then just add the argument to not save the document:
Code:
Target.Close False
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,854
Members
449,051
Latest member
excelquestion515

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