Converting VSD Drawings to PNGs – VBScript

This VBScript contains the logic to convert each drawing (page) of a specific Microsoft Visio file to a png named after its source page name and saves it to a target directory.

Option Explicit

' constants required for opening files in Visio
Const visOpenRO = 2
Const visOpenMinimized = 16
Const visOpenHidden = 64
Const visOpenMacrosDisabled = 128
Const visOpenNoWorkspace = 256

' constants required for setting ExportSize in Visio
Const visRasterFitToCustomSize = 3
Const visRasterPixel = 0


Sub export(filePath, exportDirectory, widthInPixels, heightInPixels)

    ' open file
    Dim visioApplication : Set visioApplication = CreateObject("Visio.Application")

    ' set export size
    visioApplication.Settings.SetRasterExportSize visRasterFitToCustomSize, widthInPixels, heightInPixels, visRasterPixel

    ' open document in Visio without showing it to the user
    visioApplication.Documents.OpenEx filePath, visOpenRO + visOpenMinimized + visOpenHidden + visOpenMacrosDisabled + visOpenNoWorkspace

    ' iterate over all pages and export each one
    Dim currentItemIndex
    For currentItemIndex = 1 To visioApplication.ActiveDocument.Pages.Count

        Dim currentItem : Set currentItem = visioApplication.ActiveDocument.Pages.Item(currentItemIndex)

        ' use the lowercase name for the file
        Dim exportPath : exportPath = exportDirectory & "\" & LCase(currentItem.Name) & ".png"

        ' export happens here!
    	currentItem.Export exportPath
    Next

    ' Quit Visio
    visioApplication.Quit
End Sub

' current directory
Dim currentDirectory : currentDirectory = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")

' file to open
Dim filePath : filePath = currentDirectory & "\AI - stundenplan.vsd"

' set export directory
Dim exportDirectory : exportDirectory = currentDirectory

export filePath, exportDirectory, 3557, 4114

Annotations about VBScript in general to better understand what is going on in this script.

  • The colon (:) is the statement separator. This can be used to declare and assign a variable in one line.
  • Use Dim NAME : NAME = VALUE for variables referencing not objects
  • Use Set NAME = OBJECT for variables referencing objects
  • Line Comments are started with '
  • No parantheses are allowed for calling Subs (procedures) or Functions
  • Stating Option Explicit at the first line requires each variable to be declared before it can be used
  • To determine what parameters to set, you can use the record macro function in Visio. This button is not directly available in Visio 2010, refer to this guide on how to make it visible.
  • The object explorer of Visio 2010 is very helpful to find the correct functions or procedures.
  • Use the ampersand (&) to concatenate strings
Advertisements

3 thoughts on “Converting VSD Drawings to PNGs – VBScript

  1. My first VBScript code started with yours. I modified your script to accept the visio file as an argument along with the page selection (either name or number). I’m using Visio 2007

    Thanks!

    * vsde.vbs file

    ‘Usage: cscript //NoLogo vsde.vbs /f filename
    Option Explicit
    ‘ constants required for opening files in Visio
    Const visOpenRO = 2
    Const visOpenMinimized = 16
    Const visOpenHidden = 64
    Const visOpenMacrosDisabled = 128
    Const visOpenNoWorkspace = 256

    ‘* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    ‘ current directory
    Sub Error()
    WScript.StdOut.Writeline(“Usage: cscript //NoLogo vsde.vbs /f filename “)
    WScript.Quit(1)
    End Sub

    Dim currentDirectory : currentDirectory = _
    CreateObject(“Scripting.FileSystemObject”).GetAbsolutePathName(“.”)

    ‘ file to open
    Dim filePath
    Dim fileNameSet : fileNameSet = 0

    ‘ page to open
    Dim pageName
    Dim pageNum
    Dim pageNameSet : pageNameSet = 0
    Dim pageNumSet : pageNumSet = 0

    ‘ Parse command line http://goo.gl/dn0sw
    If Wscript.Arguments.Count = 0 Then
    WScript.StdOut.Writeline(“Error: Missing arguments”)
    Error()
    Else
    Dim i
    For i = 0 to Wscript.Arguments.Count – 1
    If Wscript.Arguments(i) = “/f” Then
    filePath = currentDirectory & “\” & Wscript.Arguments(i+1)
    fileNameSet = 1
    WScript.StdOut.Writeline(“File name: ” & filePath)
    ElseIf Wscript.Arguments(i) = “/p” Then
    pageName = Wscript.Arguments(i+1)
    pageNameSet = 1
    WScript.StdOut.Writeline(“Page name: ” & pageName)
    ElseIf Wscript.Arguments(i) = “/n” Then
    pageNum = Wscript.Arguments(i+1)
    pageNumSet = 1
    WScript.StdOut.Writeline(“Page number: ” & pageNum)
    End If
    Next
    End If

    If fileNameSet = 0 Then
    WScript.StdOut.Writeline(“Error: Missing file name”)
    Error()
    ElseIf pageNameSet = pageNumSet Then
    If pageNameSet = 0 Then
    WScript.StdOut.Writeline(“Error: Missing page selection”)
    Else
    WScript.StdOut.Writeline(“Error: Only specify one page argument (name or number)”)
    End If
    Error()
    End If

    ‘ open file http://goo.gl/xl7Kp
    Dim visioApplication : Set visioApplication = _
    CreateObject(“Visio.InvisibleApp”)

    ‘ open document in Visio without showing it to the user
    visioApplication.Documents.OpenEx filePath, _
    visOpenRO + visOpenMinimized _
    + visOpenHidden + visOpenMacrosDisabled _
    + visOpenNoWorkspace

    Dim foundPage : foundPage = 0
    Dim thePage
    If pageNumSet = 1 Then
    If Int(pageNum) <= visioApplication.ActiveDocument.Pages.Count Then
    foundPage = 1
    Set thePage = visioApplication.ActiveDocument.Pages.Item(pageNum)
    Else
    WScript.StdOut.Writeline("Error: Page number out of range")
    Error()
    End If
    Else
    'Search for the
    Dim pageIndex
    For pageIndex = 1 To visioApplication.ActiveDocument.Pages.Count
    Set thePage = _
    visioApplication.ActiveDocument.Pages.Item(pageIndex)

    If thePage.Name = pageName Then
    foundPage = 1
    Exit For
    End if
    Next
    End If

    If foundPage = 1 Then
    ' set export directory
    Dim exportDirectory : exportDirectory = currentDirectory
    ' use the lowercase name for the file
    Dim exportPath : exportPath = _
    exportDirectory & "\" & thePage.Name & ".png"
    ' export happens here!
    thePage.Export exportPath
    End If

    ' Quit Visio
    visioApplication.Quit

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s