Author Topic: VBA: Easily save SVG files from drawing pages  (Read 414 times)

0 Members and 1 Guest are viewing this topic.

chrispitude

  • Jr. Member
  • **
  • Posts: 12
VBA: Easily save SVG files from drawing pages
« on: April 11, 2018, 01: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:

  • 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:

Code: [Select]
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.


Paul Herber

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2645
    • Paul Herber's website
Re: VBA: Easily save SVG files from drawing pages
« Reply #1 on: April 11, 2018, 02: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)
Electronic and Electrical engineering, business and software stencils and applications for Visio -

http://www.paulherber.co.uk/

chrispitude

  • Jr. Member
  • **
  • Posts: 12
Re: VBA: Easily save SVG files from drawing pages
« Reply #2 on: April 11, 2018, 07:20:40 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.

Nikolay

  • Hero Member
  • *****
  • Posts: 762
    • UnmanagedVisio
Re: VBA: Easily save SVG files from drawing pages
« Reply #3 on: April 12, 2018, 05: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