PDA

View Full Version : [SOLVED:] Macro to export all publisher pages as individual jpg or png



jsherk
07-05-2010, 08:37 PM
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

xavaan
07-15-2010, 05:25 AM
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?

jsherk
07-15-2010, 05:58 AM
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.

xavaan
07-15-2010, 04:43 PM
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 :)

CBell
04-20-2016, 03:03 PM
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!


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

jsherk
04-20-2016, 04:13 PM
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! :)

Eldian
06-23-2016, 02:48 AM
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".

ruvudav
11-07-2016, 06:12 AM
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.

justmeganoka
12-29-2016, 12:14 PM
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?

capistranojg
02-22-2018, 02:20 PM
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.