Visio Guy

Visio Guy Website & General Stuff => User-submitted Stuff => Topic started by: chrispitude on April 11, 2018, 06:57:26 PM

Title: VBA: Easily save SVG files from drawing pages
Post by: chrispitude on April 11, 2018, 06:57:26 PM
In my drawing files, each page is a figure, and I save it to an SVG file named after that page. I'm a keyboard shortcut guy and I got tired of going through all the Save As dialog gymnastics, so I created three macros to automate the process:


Here's the code:

Public Sub SaveThisPage()
Dim PgObj    As Visio.Page
Dim filename As String
Dim PgName   As String

'Set a handle to currently active page
Set PgObj = ActiveWindow.Page

'Skip if page is empty
If PgObj.Shapes.Count = 0 Then Exit Sub

' Select everything
Application.ActiveWindow.SelectAll

'Get Page name
PgName = PgObj.Name

'Create path to save svg file
filename = Application.ActiveDocument.Path & PgName & ".svg"

'Export the page as svg file
ActiveWindow.Selection.Export filename

'Clean Up
Set PgObj = Nothing
End Sub


Public Sub SaveAllPages()
Dim PgObj    As Visio.Page
Dim Pgs      As Visio.Pages
Dim filename As String
Dim PgName   As String
Dim iPgs     As Integer

'Set a handle to the pages collection
Set Pgs = Application.ActiveDocument.Pages

'Loop Pages collections
For iPgs = 1 To Pgs.Count
    'Set a handle to a page
    Set PgObj = Pgs(iPgs)
    ActiveWindow.Page = PgObj
    SaveThisPage
Next iPgs

'Clean Up
Set PgObj = Nothing
Set Pgs = Nothing
End Sub


Public Sub SaveAllWindows()
Dim vsoApplication As Visio.Application
Dim vsoWindows As Visio.Windows
Dim intCounter As Integer

'Get the Windows collection.
Set vsoApplication = Application
Set vsoWindows = vsoApplication.Windows

For intCounter = 1 To vsoWindows.Count
    vsoWindows.Item(intCounter).Activate
    SaveAllPages
Next intCounter
End Sub


I'm a VBA newbie, so this code might be awful and I don't know it. But hey, you get what you pay for! Also, I use Visio 2016; this might or might not work in other versions.

I put this block of code into a module called QuickSave, then I configured SaveThisPage to use Ctrl-q and SaveAllPages to use Ctrl-Q. (I run SaveAllWindows manually, whenever there is a need to regenerate everything.)

We use a shapes template file to create our drawings. I put the QuickSave module into the template file, but the macro hotkeys don't work from there. So, I then manually copy the QuickSave module from the template file to each of my drawing files. It's a pain but it works. I was able to automate this module installation method somewhat; if anyone's interested, I can share how.

Title: Re: VBA: Easily save SVG files from drawing pages
Post by: Paul Herber on April 11, 2018, 07:08:15 PM
If a shape is set to be non-selectable then it won't appear in the exported image. I don't think you need a selection anyway, the page has an export method:
PgObj.Export(filename)
Title: Re: VBA: Easily save SVG files from drawing pages
Post by: chrispitude on April 12, 2018, 12:20:40 AM
Quote from: Paul Herber on April 11, 2018, 07:08:15 PM
If a shape is set to be non-selectable then it won't appear in the exported image. I don't think you need a selection anyway, the page has an export method:
PgObj.Export(filename)

This is what I tried first. However, when I save a page (using the menus or within VBA), it includes varying amounts of whitespace margin around the objects. When I select-all and save (using the menus or within VBA), it saves just the graphic as intended.

This behavior has been in Visio for several releases. I don't know if it's intentional or not, but I've always had to do a select-all before saving a figure.
Title: Re: VBA: Easily save SVG files from drawing pages
Post by: Nikolay on April 12, 2018, 10:30:25 AM
Yep, there is a bug. You can't exclude Visio elements when programmatically exporting SVG with VBA. Probably will be fixed soon.

But yea - you can always use my export extension  ;D