Visio Export-All
- 2 minutes read - 417 wordsA little while ago, while proudly serving, I realised that I was doing a repetitive task manually. Again. This would not do. I immediately began working on a way to save myself time, by working out a better way to do this.
The task involved updating multiple Visio diagrams, and then exporting each one as a graphic file, then replacing the older ones in a Word document with the newer ones. This is a bit of a pain in the proverbial.
I did some Googling, and it turns out that VBA (previously Visual Basic for Applications) works in Visio, too. And so I created a VBA module to assist. Basically what it does is to go through a multi-page Visio document and export the bits I care about. It looks like this:
Attribute VB_Name = "ExportAllPages"
Sub ExportAllPages()
i = 0
FldrName = ThisDocument.Path
For Each pg In ThisDocument.Pages
PageName = pg.Name
PageName = Replace(PageName, "/", "__")
Debug.Print PageName
FileName = FldrName & Format(i, "000") & "-" & PageName & ".png"
If InStr(PageName, "VBackground") = 0 Then
Call pg.Export(FileName)
i = i + 1
End If
Next
End Sub What this does is to export every page to a PNG file. (If you want JPG instead, change the extension from “.png” to “.jpg”.) If the page name (what you see on the tab) has a “/” in it, it’s replaced by double underscore ("__"). There’s no error checking, because I built it originally as a me-only quick hack. It puts the resulting exported pages into the same folder as the Visio document, which is a neat way of making sure that you don’t need to worry about valid paths.
It also skips any pages that have “VBackground” in the name. I’m not sure what
those are, but I know I don’t need them.
To use this, add the code block above as a new module in your Visio document. When you run it, it’ll export each page as a PNG, printing the name as it goes in the debug area in the VBA window. You might need to tell Visio that it’s OK to load script from where you saved your Visio document, because it may not do that, depending on your security settings.
This is a mix of lots of stuff that I found all over the web, plus my own code, so I am happy to say it’s original with me. It is public domain, so you can use it as you wish.
Enjoy.