Solved

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

Posted on 2013-05-16
4
1,146 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Relationship error in table design 3 164
Flow diagrams - export sizing issue 6 56
Shape Numbering in Visio 2103? 2 131
Scale shapes without distorting 20 98
Have you ever created a custom Visio stencil – a collection of your own unique master shapes – and then created a drawing by dragging masters onto the drawing page? Have you then made changes to the stencil master and wondered why the shapes on the …
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

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