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

Run Code on Page Change/Selection

Started by vahnx, June 03, 2022, 03:47:36 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.


Hi all - I've programmed a lot in VBA/Excel but never Visio before.
In Excel I'm familiar with the Workbook Open and Worksheet Activate subroutines.
Is there an equivalent for Visio so I can run code when I open a Visio project or run code when selecting different pages/tabs at the bottom?
I found this ( but there's no example code so I'm not sure how to try it.

In the end, I'm trying to make it so "if sheet3 is clicked, then copy sheet1 and paste into sheet3 and copy2 and paste into sheet3" - basically a dynamically updating sheet that when clicked, will automatically update based on the contents of sheet 1 and sheet 2.

Thanks in advance!


Hi, there's a lot going on here.

First a little remark regarding the terminology. A sheet in Excel is a worksheet - a tab in the document / workbook. In Visio, a sheet refers to a shapesheet object (can be a shape or a page or a doc, etc.).
You were meaning page instead of sheet.

How to implement:

1 - lazy way:
Your file must be "macro-enabled" - save it as vsdm instead of vsdx.
In the IDE, there is a ThisDocument module. By means of the dropdown lists on top of the main window you can write code for the document_pagechanged event.

2 - better way:
The lazy way has in so far disadvantages as the file must be a vsdm, the code is implemented in the file instances themselves - very difficult to maintain.
Better is to move the code into a stencil (vssm) and run it from there.
I would put it in a modeless form and let it do its job as long as the form is open (and only then!).

Code for the form:
Option Explicit

Private WithEvents vWin As Visio.Window

Private Sub UserForm_Initialize()
    Set vWin = Application.ActiveWindow
End Sub

Private Sub vWin_WindowTurnedToPage(ByVal Window As IVWindow)
    'Do your operations here
    Debug.Print vWin.Page.Name
End Sub

But I wonder if you shouldn't rethink your idea.
There should be better ways to achieve whatever you're doing.
You could for instance draw on a page and use it as background for other pages.
But it depends on what your final aim is. You may want to explain this more in detail.

HTH and Rgds,


Sorry for the delay - I did rethink it a bit and came up with a better solution.
I now have 8 pages in my Visio project, 4 background floors and 4 foreground floors, with ActiveX images that when clicked, toggle my layers on/off.
It's for a network map and I currently have WAPs shown with a rudimentary heatmap that represent the signal strength on different floors.


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: 199 (show)
Files included: 34 - 1306KB. (show)
Memory used: 1054KB.
Tokens: post-login.
Cache hits: 13: 0.00123s for 26,764 bytes (show)
Cache misses: 2: (show)
Queries used: 16.

[Show Queries]