Consulting

Results 1 to 2 of 2

Thread: VBA - Save single Pages as PDF - Problems

  1. #1
    VBAX Newbie
    Joined
    Aug 2022
    Posts
    1
    Location

    VBA - Save single Pages as PDF - Problems

    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.
    Last edited by Aussiebear; 08-09-2022 at 03:51 PM. Reason: Corrected spelling mistakes

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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-...ps-tricks.html
    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
  •