Author Topic: Run Code on Page Change/Selection  (Read 495 times)

0 Members and 1 Guest are viewing this topic.

vahnx

  • Newbie
  • *
  • Posts: 2
Run Code on Page Change/Selection
« on: June 03, 2022, 10:47:36 AM »
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 (https://docs.microsoft.com/en-us/office/vba/api/visio.page.pagechanged) 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!
« Last Edit: June 03, 2022, 10:57:19 AM by vahnx »

Yacine

  • Hero Member
  • *****
  • Posts: 2952
Re: Run Code on Page Change/Selection
« Reply #1 on: June 04, 2022, 12:49:08 AM »
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:
Code
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,
Yacine

vahnx

  • Newbie
  • *
  • Posts: 2
Re: Run Code on Page Change/Selection
« Reply #2 on: June 22, 2022, 02:48:07 PM »
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.

Thanks!
« Last Edit: June 22, 2022, 02:54:14 PM by vahnx »