A newsgroup question came up a little while ago about how to automate Visio’s Save As Web function and as this is an area that I’m quite keen on, I thought I’d write a quick post about it. I’m really just going to cover what’s available in the help files but hopefully it will prove useful just reading from a different perspective.
As I mentioned, all of the documentation is available from the MSDN library, with the additional code samples found in the downloadable Visio 2007 SDK (a 2010 SDK will be available as the Visio gets its official launch in May)…
What is Save As Web?
Before I look at the code let’s just make sure we understand what we’re dealing with. The functionality of Save As Web is provided by an Add-on. The API to that add-on gives you two objects: – VisSaveAsWeb and VisWebPageSettings.
[Note – TabControl and AddTokenValuePair are obsolete in 2003 onwards]
It’s these two objects that the UI dialog (via File / Save As Web Page…), communicates with and they’re also what you talk to when you automate via code or command line. As you can see, there’s the main class, VisSaveAsWeb, which generates the output based on the settings defined in the other class. Given this separate settings class you can see how simple it would be to generate multiple output sets, perhaps targeted at desktop and mobile scenarios, in one hit.
There’s basically two methods of invoking the add-on: – via code or calling the Run method of the add-on. I’ll look at the code method first as I think makes it a little clearer, what’s going on.
Running the Save As Web Add-on – Code Method
You can use the object model to run the Save As Web add-on and although I’m just going to cover the VBA route here, there’s a useful .NET code sample in the in the downloadable SDK.
To start off, you just need to add a reference to the Microsoft Visio 14.0 Save As Web Type Library via Tools / References in the VBE. (Note – the 14.0 refers to the Visio version number so it will be 12.0 for 2007 and 11.0 for 2003.)
Once that’s done you’re ready to add the code. The following code is a mild adaptation of that found in help – I’ve just added a few extra comments to make things a little clearer and wrapped it up into a callable procedure. The way you use it is of course completely up to you…
Public Sub RunSaveAsWeb()
'Set target document
Dim vDoc As Visio.Document
Set vDoc = ActiveDocument
'Set output file name
Dim sMainDocName As String
sMainDocName = Left(vDoc.Name, InStr(vDoc.Name, ".") - 1)
Dim mainOutputFile As String
mainOutputFile = vDoc.Path & sMainDocName & ".htm"
'Call SaveAsWeb procedure
Call SaveAsWeb(vDoc, mainOutputFile)
End Sub
Public Sub SaveAsWeb(ByVal targetVisDoc As Document, _
tgtPath As String)
Dim vsoSaveAsWeb As VisSaveAsWeb
Dim vsoWebSettings As VisWebPageSettings
'Get a VisSaveAsWeb object that
'represents a new Web page project.
Set vsoSaveAsWeb = Visio.Application.SaveAsWebObject
'Get a VisWebPageSettings object.
Set vsoWebSettings = vsoSaveAsWeb.WebPageSettings
'Configure preferences.
With vsoWebSettings
'Set output formats
' Primary defaults to VML for 2003/7
' and XAML for 2010
.PriFormat = "VML"
.SecFormat = "PNG"
'Set pages to include
'.StartPage = 1 'Defaults to first (-1) page
'.EndPage = 2 'Defaults to last (-1) page
'Set UI visibility
' (Setting SilentMode to True overrides
' both QuietMode and OpenBrowser)
.QuietMode = True ' Hides user controlled dialogs
.OpenBrowser = False
.SilentMode = True
'Organise files
' TargetPath format -
' "c:\[FOLDERNAME]\[FILENAME].htm"
' StoreInFolder - adds support files to separate
' folder in following format - "[FILENAME]_files"
.targetPath = tgtPath
.StoreInFolder = True
End With
'Generate Save As Web output
' If AttachToVisioDoc is not set,
' defaults to active document
vsoSaveAsWeb.AttachToVisioDoc targetVisDoc
vsoSaveAsWeb.CreatePages ' Generate output
DoEvents
End Sub
I’m hoping that this is fairly self explanatory, but the key point is the relationship between the SilentMode, QuietMode and OpenBrowser properties and that setting the first to True overrides the last two.
Running the Save As Web Add-on – Command Line Method
An alternative to the object model method is to run the add-on using command-line options. These take the form of name/value pairs, for example:
/pagetitle=MyWebPageTitle /target=c:\mywebpage.htm
Using this method, you can run the add-on from code (here’s the example from MSDN)…
Private Sub Document_DocumentSaved(ByVal Document As IVDocument)
Application.Addons("SaveAsWeb").Run _
"/quiet=True /target=C:\temp\test.htm"
End Sub
…or via the ShapeSheet using the RUNADDONWARGS function (which if you haven’t come across it means “Run Add-on With Arguments”). With this function you could create a shape that runs the Save As Web add-on based on other conditions or events within your document.
The MSDN documentation highlights a scenario where the user can double click a shape to run the add-on. I’m just going to adapt this a little to allow the command-line options string to be dynamically generated to include the file path. I’ve added four User cells (you could cut this down, but it makes it clearer as to what’s going on) that replace the file name’s .vsd or .vdx file extension with an .htm one and then concatenates the remaining flags.
A point to note here is that the spaces preceding the forward slashes, in the command-line options, are important as they are the delimiters for the string when it gets parsed by the function. (This was a tip I picked up from David Parker some time ago.)
Further Save As Web Automation
Well I hope that adds to the available material out there on automating Save As Web. In the next few posts I’m going to be using the code method to generate Save As Web files and then programmatically edit the markup and text in the output to reduce the manual side of extending the functionality. David Parker also has a future post up his sleeve on automatically refreshing the Save As Web output files based on changes in a DataRecordset…I’ll add a link here when he’s done.