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

Posted on 2012-09-20
1 Ratings
Last Modified: 2012-09-21

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,

Question by:bedsingar
    LVL 18

    Assisted Solution

    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.

    Author Comment

    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)


    LVL 18

    Accepted Solution

    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


    Author Closing Comment

    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.

    LVL 18

    Expert Comment

    glad to assist.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
    Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
    This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
    The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now