I had a project where I needed to draw a lot of UML diagrams, and decided on Microsoft Visio for the drawing work. The diagrams had to be included in various project outputs, such as a design report and a project wiki. This would require exporting files from Microsoft Visio to PNG format whenever a […]
This article was posted by Independent Software, a website and database application development company based in Maputo, Mozambique. Our website offers regular write-ups on technical and design issues, ranging from details at code level to 3D Studio Max rendering. Read more about Independent Software's philosophy, or get in touch with Independent Software.
I had a project where I needed to draw a lot of UML diagrams, and decided on Microsoft Visio for the drawing work. The diagrams had to be included in various project outputs, such as a design report and a project wiki. This would require exporting files from Microsoft Visio to PNG format whenever a change was made to any diagram.
What I needed was a way to automate image export from Microsoft Visio. Since all the diagram files live in a single folder, a script would go through that folder, find all VSD (Visio) files, and export them to PNG.
Solution 1: using vsd2svg and ImageMagick
A free tool called vsd2svg allows us to convert any VSD file into SVG format. This worked fine for me, and I would be able to write a shell script to convert all VSD files automatically. However, I wasn’t happy with the SVG format – I wanted PNGs instead which would be nicely portable for different browsers viewing the wiki.
ImageMagick should help – it can convert SVG files into PNG. No such luck though. Although ImageMagick can do the conversion, in my case any texts were either missing or virtually unreadable. This may be linked to the use of an unknown font. However, my diagrams used Arial so that would hardly be an unknown font. ImageMagick may still work for you, though.
Solution 2: Visio Automation
Instead of using external tools, why not let Visio do all the work? Clearly I don’t want to click File -> Export -> PNG for each file in my list of diagrams, but Visio does produce excellent results when saving as PNG. The fonts are readable and Visio automatically crops any diagram to only the drawing area. Surrounding whitespace is removed.
It turns out that Visio can be automated to convert a group of VSD files to PNG in one go. This is done through same VBA scripting.
- Create a new empty diagram in Visio, called “Exporter.vsd”.
- Find the Developer Toolbar in Visio and click the “Visual Basic Editor” icon (ALT+F11 also works).
Add the following code:
' Define directory to work in:
Dim directory As String
directory = "c:\code\itc\diagrams"
' Gather .vsd files in directory:
Dim files As New Collection
Dim filename As String
filename = Dir(directory & "*.vsd")
' File names are stored without their extension:
Do While filename <> ""
filename = Left(filename, InStrRev(filename, ".") - 1)
If filename <> "Exporter" Then
filename = Dir
For Each f In files
' Open document in visio:
Dim doc As Document
Set doc = Documents.Add(directory & "" & f & ".vsd")
' Export each page as PNG:
Dim p As Page
For Each p In doc.Pages
Dim output As String
output = directory & "" & f & "-" & p.Index & ".png"
This code will:
- Find all VSD files in the specified working directory and store them in a list (except for our file Exporter.vsd);
- Find all pages within each file;
- Export each page as a PNG file.
This macro can now be connected to a button on a toolbar, or simply executed from the Visual Basic editor. It will convert all VSD files to PNG files in one go. The resulting images can then immediately be uploaded to a web server for use in a wiki, or could be linked from a Word file.