VBA macro to save pages of a word doc to individual images


I'm wondering if it is possible to create a VBA macro for Word 2010 that exports specific pages of a word document based on the bookmark on that page as individual jpeg images?

I.E a word document has 40 pages, 10 of those pages have "Export1", "Export2" ...."Export10" bookmarks on them.

I want a VBA script that exports each of the 10 pages as BMK1.JPG, BMK2.jpg... BMK10.jpg etc

I then plan to use these pages as background images on a layered webpage, with an input box over the area that users need to complete. - As we update the images, the backgrounds of the pages will change and so the user will have a sound basis to complete their commentary input.

Using this method is far from ideal, but we don't have much of an alternative at the moment.

Thanks in advance,

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RartemassService Desk AnalystCommented:
As a quick way you can print the specific pages to a PDF printer (Adobe Elements or free 3rd party app). Then from within Adobe Acrobat you should be able to extract all pages as individual ones.
If you require jpg files you can then do a batch conversion with Irfanview.

Programming this in VBA would be quite time consuming.
If you really want to go via that method then you will most likely need to make a selection bookmark, ie highlight the entire page and make that a bookmark.
You could then print the bookmark

The below macro (not my work) generates a list of all bookmarks at the end of the active document, together with their contents. Assuming the bookmarks are selections then it should display all the pages you want.

Sub ListBkMrks()
Dim oBkMrk As Bookmark
If ActiveDocument.Bookmarks.Count 0 Then
With Selection
.EndKey Unit:=wdStory
.TypeText Text:=vbCrLf & "Bookmark" & vbTab & "Contents"
For Each oBkMrk In ActiveDocument.Bookmarks
.TypeText Text:=vbCrLf & oBkMrk.Name & vbTab & oBkMrk.Range.Text
Next oBkMrk
End With
End If
End Sub

The above displays the bookmarks at the end of the document, you could also tell it to display in a new document to print with another VB command, or simply print from the above and once complete either exit without saving or delete the repeated pages before ending the script.
This can then be printed to the PDF printer, or if you create a new document it can be saved as PDF by Word.
bedsingarAuthor Commented:
Due to the environment, thirdparty software isn't an option for me (unfortunately!) ... the content on the pages is essentially a bunch of linked charts from Excel.

it occurred to me that it may be easier to use VB to export each of those objects, charts / cell ranges as images rather than trying to print the entire page in word.

Is there a script available that would do this?

(I'll move this to a new question if deemed more appropriate)


RartemassService Desk AnalystCommented:
Being a chart you can try the below. It works in Excel to convert a chart to an image. haven't tested it in Word.

Public Enum PicType
    GIF = 1
    JPG = 2
    BMP = 3
End Enum
Sub SaveChartAs(ByRef Chart_Object As ChartObject, ByVal PictureType As PicType, _
                Optional ByVal FilePath As String, Optional ByVal FileName As String)
    Dim Extn    As String
    Extn = Application.Choose(PictureType, ".GIF", ".JPG", ".BMP")
    If Len(FilePath) = 0 Then FilePath = ThisWorkbook.Path
    If Len(FileName) = 0 Then
        FileName = Chart_Object.Chart.Name & Extn
        FileName = FileName & Extn
    End If
    Chart_Object.Chart.Export FilePath & "\" & FileName, Mid$(Extn, 2)

End Sub

Open in new window

Then call the above where you need it.

Sub kTest()
    SaveChartAs ActiveSheet.ChartObjects(1), JPG, "C:\Test", "MyImage"
End Sub

Open in new window


Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bedsingarAuthor Commented:
Thanks for your input, the first example would have done what I originally asked, but the excel script is how I took things forward. I'll export the charts directly from Excel rather than from the word page.

RartemassService Desk AnalystCommented:
glad to assist.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.