• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2488
  • Last Modified:

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,

  • 3
  • 2
2 Solutions
RartemassAuthor, martial arts coach, IT ConsultantCommented:
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)


RartemassAuthor, martial arts coach, IT ConsultantCommented:
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

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.

RartemassAuthor, martial arts coach, IT ConsultantCommented:
glad to assist.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now