• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1310
  • Last Modified:

how to detect a change in active page as an 'event'

In excel, the event Workbook_SheetActivate enables one to detect a change in the active sheet, without having to run standard module code to detect the active sheet.

There is no such event in Visio.

Is there a work-around to enable one to detect (as a type of event) a change in active page in Visio?
0
Kelvin4
Asked:
Kelvin4
  • 3
1 Solution
 
Visio_GuyCommented:
Hi Kelvin,

Visio has two major trees that you have to consider: the doc tree and the window tree. These two points sort of illustrate how it works:

1. App.Documents.Document.Pages.Page.Shapes.Shape.Cells.Cell
2. App.Windows.Window.Page  OR App.Windows.Window.Selection.Shape

Visio has two events that work on Window, Windows and Visio Application objects:

- BeforeWindowPageTurn
- WindowTurnedToPage

Here's some code. Copy the whole block and put it in ThisDocument for a multi-page, blank document. You will get rectangles all over the place, so make sure it's a test doc!

You can start and stop the events by clicking the blue "Run/Design Mode" triangle in the VBA interface.

The comments in the code explain a lot more than I have written here.

Cheers,

Chris

Option Explicit

Dim WithEvents m_visWins As Visio.Windows

Private Sub Document_RunModeEntered(ByVal Doc As IVDocument)
  
  '// Better than on doc opened or on doc created,
  '// this happens in both cases, plus when you re-enter
  '// run mode.

  Set m_visWins = Visio.Application.Windows

End Sub

Private Sub m_visWins_WindowTurnedToPage(ByVal visWin As IVWindow)

  '// visWin is the the window, as it is AFTER
  '// the page has been turned.

  '// See also the event: BeforeWindowPageTurn
  
  '// This captures a page turn. visWin will be the
  '// active window, if a user is doing the actions.
  '// However, if code changes the page for a window,
  '// then visWin doesn't need to be the active window
  
  '// You can double-check that visWin is really for
  '// a drawing. But it likely is, judging by the event name.
  '// However, you might want to allow or disallow group windows,
  '// or master editing windows.
  
  '// Only react to drawing windows:
  If (visWin.Type = Visio.VisWinTypes.visDrawing) Then
    '//... do something
    Debug.Print "WindowTurnedToPage: " & visWin.Caption
  Else
    Debug.Print "WindowTurnedToPage, but not a drawing window: " & visWin.Caption
  End If
  
  '// Note if visWin is Visio's active window:
  If (visWin Is m_visWins.Application.ActiveWindow) Then
    Debug.Print vbTab & "-Page turned for active window."
  Else
    Debug.Print vbTab & "-Page turned for window other than active window."
  End If
  
  Call m_drawShape(visWin, _
                   "Got here before you!")
  
End Sub

Private Sub m_visWins_BeforeWindowPageTurn(ByVal visWin As IVWindow)

  '// Note: visWin.Page holds the page that
  '// visWin shows BEFORE the page is turned!

  Call m_drawShape(visWin, _
                  "This is what you left behind!")
  
End Sub

Private Sub m_drawShape(ByRef visWin As Visio.Window, _
                        ByVal sMessage As String)

  If (visWin.Type <> Visio.VisWinTypes.visDrawing) Then Exit Sub
  
  '// Pick some random coordinates:
  Dim dx As Double, dy As Double
  Call VBA.Math.Randomize
  dx = VBA.Math.Rnd() * 7.5 + 0.5
  dy = VBA.Math.Rnd() * 10 + 0.5
  
  '// Get the page for the window:
  Dim pg As Visio.Page
  Set pg = visWin.Page
  
  '// Make a rectangle, add some text:
  Dim shp As Visio.Shape
  Set shp = pg.DrawRectangle(dx - 0.5, dy - 0.5, dx + 0.5, dy + 0.5)
  shp.Text = sMessage & vbCrLf & _
             Format(Now(), "yy.mm.dd hh:mm:ss")
  
  '// Cleanup:
  Set shp = Nothing
  Set pg = Nothing
  
End Sub

Open in new window

0
 
Kelvin4Author Commented:
Chris,
Many thanks. It will take me a day or so to try this out, but it will be my first priority.
Looks like huge help.

Kelvin
0
 
Kelvin4Author Commented:
This really was excellent help, not just for knowledge, but a really helpful coded example.
Saved me hours!

Thanks!
Kelvin
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now