" /> Visio Guy » Export All Pages in Document
Home » Code

Export All Pages in Document

Submitted by on September 18, 2006 – 8:30 pm | | 62494 views 27 Comments

Every once in a while, a question comes along in the newsgroups that just screams at me; “WRITE SOME SAMPLE CODE!” Now I can’t describe exactly which types of questions speak to me in this way. Perhaps they’re questions I’ve heard time and again? Or they involve a fascinating graphical problem? Or is it just that I can actually finish them in one evening’s work?Whatever the reason, the latest question is; “How do I export all of the pages in a Visio document to image files?”

Export All Pages in Visio Document

Well, the simple answer is to use the Page.Export function, and simply give the name of an image file as an argument.

For instance: To do all the pages in a document simply requires a loop through each Page object in Document.Pages. To do all the pages in a document simply requires a loop through each Page object in Document.Pages.

But that’s only half the answer.

The big bone about programatically exporting images in Visio is that you can’t programmatically control the resolution of the output. I believe this is because the export filters (originally?) came from third parties, and didn’t necessarily have automation interfaces. But all is not entirely lost. If you manually export an image, you’ll see this dialog:

Visio Export Filter Options

You can set various options for the export, including the resolution, which is what most folks want to do. This seems all well and good, except for the fact that you can’t get at it programmatically, and even worse, Visio forgets your settings every time you close Visio.With all these limitations in mind, it seems like we’re left with no choice but a work-around that looks like this:

1. Manually export an image using SaveAs.
2. Set the resolution in the dialog.
3. Before shutting Visio down, run any automation code to export multiple images.

And that is exactly what the VBA code in the companion download file does.

Note: Visio 2010 has expanded the API for exporting images from Visio. Among the new features is programmatic control over export resolution. See: New Visio 2010 API for Configuring Raster Export

When your run the sub ExportAllPages either via the VBA interface, or by clicking on the big button sitting at the bottom of Page-1, you’ll see a funny dialog that lets you choose your export image type: .bmp, .gif, .jpg, .png or .tif.

Export Dialog

Once you’ve selected an image type, you’ll be presented with the SaveAs dialog:

This is your big chance to choose your export settings. In the File name field, type in an image file name such as test.gif, test.bmp, test.png, etc. (but not test.vsd!) Just make sure the extension on your dummy file matches the extension you picked in the first dialog!

After you’ve done this, the code will take over and export every page in your document to the image type you’ve selected. The files will be in the same directory as the .vsd file, and will be prefixed with the number of the page: 001, 002, 003, etc.

For those who just want to get the loop going, here’s a simpler version of the export code, without all the dialogs:

[code lang=”vb”]
Public Sub

Dim i As Integer
Dim formatExtension As String formatExtension = ".gif" ‘…or .bmp, .jpg, .png, .tif

‘// Init folder, doc and counter:
folder = ThisDocument.Path
Set doc = Visio.ActiveDocument
i = 1

‘// Loop through pages:
For Each pg In doc.Pages

‘// Setup the filename:
filename = Format(i, "000") & " " & pg.Name

‘// Append ‘(bkgnd)’ to background pages:
If (pg.Background) Then filename = filename & " (bkgnd)"

‘// Add the extension:
filename = filename & formatExtension

‘// Save it:
Call pg.Export(folder & filename)

i = i + 1


End Sub

[/code] (51.32 KB) - 2801


  • Liam says:

    This is great!

  • Bobby Dazzler says:

    Quality little bit of code. Spot on. Saved me 30 x “Save As…” boredom (plus future occasions of the same task). Cheers. Bobby.

  • Diwakar says:

    Excellent Script!

  • Paul Irish says:

    Quick tip.
    Make sure this vsd file is saved to a real place on the harddrive, not a temp folder.
    Also, even better if its in the same folder as the other VSD you’re processing.

  • DurzoFlint says:

    How can I modify this to save a selected drawing to a specific path and as a png file.



  • Visio Guy says:

    Hi Durzo,

    Just change a few lines, for example:

    Dim formatExtension As String formatExtension = “.png”

    folder = “C:\DurzosExports”

    – Chris

  • Rick Bond says:

    Dumb question:

    When I click the “Export All Pages” button in “Export All Pages.vsd, it exports only the Export All Pages” image.

    How do I tell it to export all the pages in MY document?

    Thank you,


  • Visio Guy says:

    Hi Rick,

    Make the document you want to export the “active” document. Ie: give it focus and minimize the “exporter”.

    Then, go into the VBA project and look for ThisDocument.ExportAllPages. Place your cursor somewhere in this procedure, then press F5 to run it.

    The code gets the ActiveDocument and exports that. When you press the button, then the exporter document is always active, so if using the button, you’re kind of stuck.

    But when running from the code window, any document can be active, so this is more flexible.

  • david says:

    Visio Guy, you are the BOMB!!! Thanks for saving me time and pain. May Karma repay you. 😀

  • Mana says:


    Please tell me more detail about how can I create the button Export all in my new Visiso document and insert the VB code?

    I tried to copy the button in demo file (Export All Pages.vsd) to my Visio file, but nothing happen when clicking on it.

    Thank you.

  • Visio Guy says:

    Hi Mana,

    Just copying the button doesn’t copy all the code behind it. You probably just end up with another button that is linked to nothing.

    Here’s roughly how to hook a button to VBA code. After adding a button to a Visio page:

    1. Right click the button
    2. Choose CommandButton Object > View Code
    3. In the VBA editor that appears, add the code from this article either by referencing sub ExportAllPages, or copying the code and pasting it into the Button’s procedure.

  • jabbett says:

    Looks like Visio 2010 has more API hooks for configuring export formats:

    New Visio 2010 API for Configuring Raster Export

  • Visio Guy says:

    Thanks jabbett,

    Yup, Visio 2010 finally has added some control over export resolution!

  • Mana says:

    Thank you Visio Guy!
    Some small issues:

    formatExtension = “.gif” ‘…or .bmp, .jpg, .png, .tif
    formatExtension = “.gif”
    ‘//…or .bmp, .jpg, .png, .tif

    Set doc = Visio.ActiveDocument i = 1
    Set doc = Visio.ActiveDocument
    i = 1

  • Visio Guy says:

    Hi Mana,

    Fixed the second typo, not sure what your first comment was about…those are just comments!

  • Lantrix says:

    Mana’s first comment was in essence this:

    Change this:
    <br />
    Dim formatExtension As String formatExtension = ".gif" '...or .bmp, .jpg, .png, .tif<br />

    to this:
    <br />
    Dim formatExtension As String<br />
    formatExtension = ".gif"<br />
    '//…or .bmp, .jpg, .png, .tif<br />

  • Rob Pearson says:

    Many thanks for this. Your comment from April 10 2009 was essential to understand how to get this to work, so that should probably be in the main body of the article – don’t overestimate our level of knowledge!

    Also the export falls over if you have any disallowed characters in your page names – I had colons and it took me a while to figure out that I needed to remove them.

    Thanks again 🙂

  • Visio Guy says:

    Thanks Rob,

    TODO: replace characters from page names that will be illegal in file names.

  • Pam says:

    This is definitly great. Have you tried publishing web pages by looping through the pages? Is this possible?
    thank you

  • ro says:


    I am running the code and it is only exporting 8 sheets (my document has over 60 sheets which is why this macro would be ÜBER helpful!!)

    any help?


  • Visio Guy says:

    Hi RO,

    I wonder if the export is taking too long and VBA is tripping over itself?

    Maybe try adding a DoEvents call after pg.Export, or put a breakpoint there and keep F5-ing to it to see if you get further than 8 pages (ie: give the system time to catch up)

  • Visio Guy says:


    Yes, you could loop through pages and publish as web, but Visio’s Save As Web Page functionality automatically creates a mini-web-site with all of the pages in a document for you!

  • Ken says:

    You sir, are the man! Your nifty bit of code saved me hours and hours of work, thank you so much.

    If you are ever in Washington, DC please shoot me an email so that I may buy you all the drinks.

    Eternal Gratitude,


  • Israel Anthony Lopez says:

    Visio Guy,

    I am having an issue in Microsoft Visio 2013 and get an error at:

    Call pg.Export(folder & filename)

    The funny thing is that it imports about half the pages then stops on my 12 page Visio diagram.

  • sputnik says:

    Hi Visio Guy,

    thanks a lot from here as well, it is a crazy useful bit of code!

    I believe that since the new version of Visio has been around for quite some time now, you have no intention to enhance this snippet, but if you happen to have come across the topic of applying transparancy settings via the API, I would appreciate if you could point me to some docs.

    Unfortunately I am forced to stick to Visio 2007, and whereas the script itself if spot on for batch export, transparency setting is simply not applied, and by googling I could not find any solution to apply it via the standard API.

    Keep it up, BR,

  • Visio Guy says:

    Hi ???????,

    Re: transpaerncy: be sure to check out:

    The Hidden World of Visio Shapes

  • sputnik says:


    thanks again; it is yet another very useful addition to my superficial knowledge about Visio.

    However I missed one important aspect in my previous comment: my intention is to apply transparency for the page/background of each sheet (which I am doing manually during exporting to images by setting transparency for white), but the ShapeSheet of does not seem to have any option for that (opposed to shapes or images, where there are parameters for this). If you have any further hints, they would be still appreciated!

    Thanks, BR,

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" extra="">

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.