Solved

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

Posted on 2013-05-16
4
1,074 Views
Last Modified: 2013-05-21
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
Comment
Question by:Kelvin4
  • 3
4 Comments
 
LVL 11

Accepted Solution

by:
Visio_Guy earned 500 total points
ID: 39174246
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
 

Author Comment

by:Kelvin4
ID: 39175669
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
 

Author Closing Comment

by:Kelvin4
ID: 39176225
This really was excellent help, not just for knowledge, but a really helpful coded example.
Saved me hours!

Thanks!
Kelvin
0
 

Author Comment

by:Kelvin4
ID: 39186566
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Question Several times in recent years, someone has posted a question at EE asking whether they could display the Windows directory structure – folders and subfolders – using Visio. When one person specifically asked about displaying directory …
The ability to add structure to Visio diagrams using containers, lists and callouts is one of my favorite features in Visio 2010. In this article we’ll examine containers. We’ll explore lists (http://www.experts-exchange.com/Microsoft/Applications/M…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now