Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-05-16
4
Medium Priority
?
1,274 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 2000 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

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

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 containers. We’ll explore lists (http://www.experts-exchange.com/Microsoft/Applications/M…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

610 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