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:

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.

Comments

8 8 Responses to “Automated conversion of many Visio drawings to PNG”
  1. Milt says:

    I found this doing a search for ways to batch convert about 400 visio files to png. Unfortunately I am getting a compile error. I am not a programmer so not sure what is wrong. Here is the offending line:

    Set doc = Documents.Add(directory& “” & f & “.vsd”)

    Any suggestions?

    • alex says:

      There seems to be a typo. It should be

      That is, there’s a space after directory. I’ve fixed this in the listing above.
      Does that help?

  2. Jack DeLand says:

    This is brilliant. I looked all over the Web for a batch converter, and this is terrific.

  3. Russell says:

    Copied and pasted the text above and there were a few errors/typos with the version of VBS Editor / Visio I was using. This was awesome, credit and thanks to whoever wrote this up.

    MS VBS for Applications 7.0
    Visio 14.0 (2010 Premium – 32bit)
    Discovered issues
    The ‘amp;’ after every & needed to be removed.
    The ‘<> in the statement Do While filename and If filename statements — needed to be

    If filename <> “Exporter” Then

    Sub ExportPNGs()
    ‘ Define directory to work in:
    Dim directory As String
    directory = “c:codeitcdiagrams”
    ‘ 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
    files.Add (filename)
    End If
    filename = Dir
    Loop
    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”
    p.Export (output)
    Next
    doc.Close
    Next
    End Sub

  4. vijaybhaskar says:

    Is it possible to convert the visio stencils(i.e., .vss) to individual images using java/javascript

  5. Tad says:

    Pretty awesome the way the code gets mangled by the website.

  6. Neeraj says:

    Can this be used in a Java environment, where in it converts VSD to SVG to PNG/JPEG etc.?
    can I write a Java program to call a bash script that will use vsd2svg module and then use Apache Batik or ImageMagik to get JPEG / PNGs?

Leave a Reply

Your email address will not be published. Required fields are marked *