Menu

Exporting All Diagrams From Visio

by Jason Williscroft

This is just too useful not to share.

I was preparing an article this evening, and wanted to use a bunch of diagrams from a Visio design document. As it turns out, exporting PNG files from Visio is a pain. Once is no big deal, but there were eighteen diagrams in this thing. I wanted automation.

So Google did what Google does, and I found a sweet Visio plugin that does just that: automagically exports all your diagrams. Nice.

But I’m lazy. I didn’t even want to unzip and install a Visio plug-in. And this was the moment when Visio Guy showed us how to do it… because if you scroll to the bottom of the article, he also presents a short script that does exactly the same thing, but without all the dialogs and plug-ins and junk!

Just copy & paste into your Visio VBA editor, maybe change the file type if you want a different one, and then run the macro. Done.

How cool is that?

For the truly lazy, I’ll save you a click. Here’s the script:

Public Sub ExportAllPages()
    Dim formatExtension As String
    formatExtension = ".png" '...or .bmp, .jpg, .gif, .tif
 
    ' Init folder, doc and counter:
    Dim filename As String, folder As String
    folder = ThisDocument.Path
 
    Dim doc As Visio.Document
    Set doc = Visio.ActiveDocument
 
    Dim i As Integer
    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
 
Cleanup:
    Set doc = Nothing
End Sub
Previous Post Announcing hqRule
Next Post Some Thoughts on Design