Export All Pages in Document
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?”
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:
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.
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:
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 Next End Sub