Solved

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

Posted on 2013-05-16
4
1,043 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Periodically someone asks me whether there’s a way to automatically convert all of the pages in a Visio drawing to PowerPoint slides. There have even been a few times when I’ve wanted to do that myself but I never really had enough incentive to figu…
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 lists. We’ll explore containers and callouts in separate articles. Prior to reading th…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

18 Experts available now in Live!

Get 1:1 Help Now