BB code in posts seems to be working again!
I haven't turned on every single tag, so please let me know if there are any that are used/needed but not activated.

Main Menu

VBA: Easily save SVG files from drawing pages

Started by chrispitude, April 11, 2018, 06:57:26 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.


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:

  • SaveThisPage – saves the current drawing page as an SVG file named after the page name.
  • SaveAllPages – similarly saves all pages in the current Visio drawing document.
  • SaveAllWindows – similarly saves all pages of all drawing documents open in the current Visio instance.

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

'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
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
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.

Paul Herber

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:
Electronic and Electrical engineering, business and software stencils for Visio -


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:

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.


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

Browser ID: smf (possibly_robot)
Templates: 4: index (default), Display (default), GenericControls (default), GenericControls (default).
Sub templates: 6: init, html_above, body_above, main, body_below, html_below.
Language files: 4: index+Modifications.english (default), Post.english (default), Editor.english (default), Drafts.english (default).
Style sheets: 4: index.css, attachments.css, jquery.sceditor.css, responsive.css.
Hooks called: 215 (show)
Files included: 32 - 1207KB. (show)
Memory used: 1066KB.
Tokens: post-login.
Cache hits: 14: 0.00131s for 26,764 bytes (show)
Cache misses: 3: (show)
Queries used: 16.

[Show Queries]