PDA

View Full Version : [SOLVED:] VBA - Save single Pages as PDF - Problems



Fendroun
08-09-2022, 10:29 AM
I want to save single pages from a series letter. Here is my code so far (based on code I found, can't remember where). The bold line is the most important one:


Sub SavePages()

Dim MainDoc As Document, PageDoc As Document
Dim PageNumber As Long

Set MainDoc = ActiveDocument

PageNumber = 1
Application.ScreenUpdating = False

MainDoc.Range(0, 0).Select

Do
Selection.Bookmarks("\Page").Select
Selection.Copy

Set PageDoc = Application.Documents.Add(Visible:=False)
PageDoc.Range.Paste
PageDoc.SaveAs "Page " & PageNumber & ".docx"
PageDoc.Close

If PageNumber = MainDoc.Range.Information(wdNumberOfPagesInDocument) Then _
Exit Do
Selection.GoToNext wdGoToPage
PageNumber = PageNumber + 1
Loop

Application.ScreenUpdating = True
Set PageDoc = Nothing
Set MainDoc = Nothing

End Sub


The code works, I get single pages, but i have following problems

- The pages are not exactly like in the original document, for example the lines are spaced differently. That's why they don't fit on a single page anymore, which they do in the original document.

- Every single-page-document has an extra blank page at the end which I don't want


I want to save them as pdfs, but when I try


PageDoc.SaveAs "Page " & PageNumber & ".docx"

I get pdf docs, but they are damaged so I am not able to open them. I guess SaveAs is not for pdfs?

Last problem: I don't want the documents to be named "Page x", but they should be named like the text in a cell (2,2) from table(1).


PageDoc.SaveAs tables(1).cell(2,2).range.text & .docx"

When I try this, I get this error: Word cannot complete the save due to a File Permission Error.

But imo this is not a Document Problem, because when I try something like


PageDoc.SaveAs PageDoc.Words(2) & ".docx"

pages are being generated. So I guess it has to do something with the table, but I dont get it.

I would be very greateful for any help. I am a VBA beginner, but in Excel, this is the first time I use it for Word.

macropod
08-11-2022, 07:36 PM
It seems your post concerns documents produced via mailmerge. In that case, see Split Merged Output to Separate Documents or, better still, Send Mailmerge Output to Individual Files in the Mailmerge Tips and Tricks page at: https://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html