Consulting

Results 1 to 10 of 10

Thread: Macro to export all publisher pages as individual jpg or png

  1. #1

    Macro to export all publisher pages as individual jpg or png

    There is not much info out there on VBA programming for Publisher (I had a hard time finding it anyway), and I needed to export each page in a publisher file as an individual graphic (jpg or png). Although you can do a Save As and save each page one at a time, that becomes a little tedious, so I wrote this macro to export all the pages individually as a graphics file.

    Hope this is helpful to somebody!

    Sub Export_All_Pages_As_Graphic()
    'Ask whether to proceed or not
    BtnPress = MsgBox("Save all pages as individual pictures?", vbOKCancel)
    'If they pressed OK then proceed, otherwise do nothing.
    If BtnPress = 1 Then
       'Make sure two-page spread is set to FALSE, otherwise it might export the two pages as one picture, instead of individually.
       'NOTE: This may not be necessary, but just in case!
       ActiveDocument.ViewTwoPageSpread = False
       'How many total pages in document?
       TotalPages = ActiveDocument.Pages.Count
       'Loop thru all pages one at a time
       For PgCnt = 1 To TotalPages
          'When you convert integer to string it adds a space in front, so need to remove the leading space
          PgNumber = Str(PgCnt)
          PgLen = Len(PgNumber)
          PgNumber = Right(PgNumber, PgLen - 1)
          'All names are three characters with leading zeros like 002.png 014.png 123.png
          If PgCnt < 10 Then
             PgFilename = "00" + PgNumber
          ElseIf PgCnt < 100 Then
             PgFilename = "0" + PgNumber
          Else
             PgFilename = PgNumber
          End If
          'Add the appropriate fileformat extension. You can use .png and .jpg for sure. There may be others, but did not test any.
          PgFilename = PgFilename + ".png"
          'Save the page
          ActiveDocument.Pages(PgCnt).SaveAsPicture (PgFilename)
       Next PgCnt
       'Tell them you are done and how many pages were saved
       MsgBox "DONE! Saved" + Str(TotalPages) + " pages."
       End If
    End Sub
    Last edited by Aussiebear; 04-09-2023 at 04:39 PM. Reason: Reduced the whitespace

  2. #2
    VBAX Newbie
    Joined
    Jul 2010
    Posts
    2
    Location
    I signed up to this forum just to say thanks! I have been trying to work out how to do this for a year! You are absolutely awesome!!!!

    Can I stretch your knowledge and ask if you can tell me how to adjust the code to save the jpegs in 300dpi resolution?

  3. #3
    I found a couple links, but I cannot post them because I don't have enough posts in the forum, so hoefully you can decipher them from the following:

    A quick google found this:
    msdn [dot] microsoft [dot] com/en-us/library/aa438329%28office.12%29.aspx

    And this:
    msdn [dot] microsoft [dot] com/en-us/library/aa438993%28v=office.12%29.aspx

    So in theory, adding a ,3 after PgFilename should work like this:

    'Save the page
            ActiveDocument.Pages(PgCnt).SaveAsPicture (PgFilename, 3)
    I have not tested this though, so give it a try and post back whether it works or not.

  4. #4
    VBAX Newbie
    Joined
    Jul 2010
    Posts
    2
    Location
    Hi, thanks for getting back so quick. It works perfectly except the ,3 has to be outside the parenthesis.

    Ive never used a macro successfully before so thanks again

  5. #5
    VBAX Newbie
    Joined
    Apr 2016
    Posts
    1
    Location

    AWESOME! Totally worked.

    OMG, I am SOOOOO glad I found this (pretty old) thread!

    I had a 631-page Publisher file I needed to get into PowerPoint for a slideshow and was beginning to have a panic attack at the idea of save each page as a JPEG one at a time! But, this worked perfectly and you helped me out tremendously. Thank you for posting it!

    Quote Originally Posted by jsherk View Post
    There is not much info out there on VBA programming for Publisher (I had a hard time finding it anyway), and I needed to export each page in a publisher file as an individual graphic (jpg or png). Although you can do a Save As and save each page one at a time, that becomes a little tedious, so I wrote this macro to export all the pages individually as a graphics file.

    Hope this is helpful to somebody!

    Sub Export_All_Pages_As_Graphic()
    'Ask whether to proceed or not
    BtnPress = MsgBox("Save all pages as individual pictures?", vbOKCancel)
    'If they pressed OK then proceed, otherwise do nothing.
    If BtnPress = 1 Then
    'Make sure two-page spread is set to FALSE, otherwise it might export the two pages as one picture, instead of individually.
    'NOTE: This may not be necessary, but just in case!
       ActiveDocument.ViewTwoPageSpread = False
       'How many total pages in document?
       TotalPages = ActiveDocument.Pages.Count
       'Loop thru all pages one at a time
       For PgCnt = 1 To TotalPages
          'When you convert integer to string it adds a space in front, so need to remove the leading space
          PgNumber = Str(PgCnt)
          PgLen = Len(PgNumber)
          PgNumber = Right(PgNumber, PgLen - 1)
          'All names are three characters with leading zeros like 002.png 014.png 123.png
          If PgCnt < 10 Then
             PgFilename = "00" + PgNumber
             ElseIf PgCnt < 100 Then
             PgFilename = "0" + PgNumber
             Else
             PgFilename = PgNumber
          End If
          'Add the appropriate fileformat extension. You can use .png and .jpg for sure. There may be others, but did not test any.
          PgFilename = PgFilename + ".png"
          'Save the page
          ActiveDocument.Pages(PgCnt).SaveAsPicture (PgFilename)
       Next PgCnt
       'Tell them you are done and how many pages were saved
       MsgBox "DONE! Saved" + Str(TotalPages) + " pages."
    End If
    End Sub
    Last edited by Aussiebear; 04-09-2023 at 04:34 PM. Reason: Adjusted the code tags

  6. #6
    I posted this code about 6 years ago, and had completely forgotten about it, so thanks for the thank you!! Glad it worked for you!

  7. #7
    VBAX Newbie
    Joined
    Jun 2016
    Posts
    1
    Location
    Quote Originally Posted by jsherk View Post
    I posted this code about 6 years ago, and had completely forgotten about it, so thanks for the thank you!! Glad it worked for you!
    Thank you, this post has been very useful to me too. It gave me the basic knowledge about objects and function then I made a few changes :
    Sub SaveAsPNG()
    Dim oPage As Page
    Dim sBaseFileName As String
    Dim sPageFileName As String
    Dim bViewTwoPageSpread As Boolean
    bViewTwoPageSpread = ActiveDocument.ViewTwoPageSpread
    ActiveDocument.ViewTwoPageSpread = False
    sBaseFileName = ActiveDocument.FullName
    sBaseFileName = Left(sBaseFileName, InStrRev(sBaseFileName, ".pub", -1, vbTextCompare) - 1)
    For Each oPage In ActiveDocument.Pages
        sPageFileName = sBaseFileName & "_Page" & Right("000" & oPage.PageNumber, 3) & ".png"
        oPage.SaveAsPicture sPageFileName, pbPictureResolutionCommercialPrint_300dpi
    Next oPage
    ActiveDocument.ViewTwoPageSpread = bViewTwoPageSpread
    End Sub
    Every PNG picture is saved in the same folder as the publisher document using the same name adding "_Pagennn.png".
    Last edited by Aussiebear; 04-09-2023 at 04:30 PM. Reason: Remove extra whitespace

  8. #8
    VBAX Newbie
    Joined
    Nov 2016
    Posts
    1
    Location
    Very useful thread.

    But I'm too newbie to know how to use it. Where do I copy this code into Publisher, for it to work? Or is it Command Line code?

    Some step-by-step instructions on using this code inside Publisher would be very helpful.

    Thanks much.

  9. #9
    So I've been using this for a few months now to help out a client at my work, and it's been great, but recently he got an update to Publisher and it doesn't seem to work anymore! Any tips?

  10. #10
    Quote Originally Posted by CBell View Post
    OMG, I am SOOOOO glad I found this (pretty old) thread!

    I had a 631-page Publisher file I needed to get into PowerPoint for a slideshow and was beginning to have a panic attack at the idea of save each page as a JPEG one at a time! But, this worked perfectly and you helped me out tremendously. Thank you for posting it!

    Thank you so much! It worked almost the first time and I also use Publisher to design certain presentations then need to output to jpgs for a cool slide show in another program with zoom effects. This saved a couple hours of tedious work. I like that PowerPoint asks if you want all or just one slide saved as jpgs. Thanks again! I learned a bunch about macros in the process.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •