Search code examples
c#.netadd-invisiooffice-addins

Visio: catch switching window/document event


I developed an add in for Visio and I'm having some trouble with catching certain events. I have a custom ribbon for my add in and what I already can do, is enabled or disabled (show/hide) it based on the document.

But now I would like to have multiple documents open and check when switching to another document if the ribbon should be enabled or disabled. However I can not seem to find the right event code for this. I tried the following codes but with no luck:
- PageChanged
- WindowChanged
- BeforeWindowPageTurn
- WindowTurnedToPage

So concretely what I would like to do is catch the event when one document loses focus and another one gets the focus.


Solution

  • You could listen to the Application.WindowActivated event and then in the handler, inspect the ActiveDocument or the window.Document property (in the eventargs) to decide whether it is a document that you're interested in. You might also want to listen to DocumentCreated and DocumentOpened (on Application) and only start listening to the window event once you know that the application might be handling one of your documents.