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 = VALUEfor variables referencing not objects - Use
Set NAME = OBJECTfor variables referencing objects - Line Comments are started with
' - No parantheses are allowed for calling
Subs (procedures) orFunctions - Stating
Option Explicitat 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