Usually what is happening when someone asks this question is they are not rewiring the events when the document changes or they are not rewiring the events correctly. When the document changes, you should unwire all event listeners that were added using the previous document and then rewire those event handlers using the new document. If you don't, then your event handlers are still listening to the previous document's events and will cease to fire since that document is no longer the current document. Here's an example of how to do it correctly:Declarations Private m_documentEvents As IDocumentEvents_Event Private m_activeViewEvents As IActiveViewEvents_Event
Extension Startup routine where events are initially wired Dim mxDocument As IMxDocument = DirectCast(ESRIContainer.Instance.Application.Document, IMxDocument) m_documentEvents = DirectCast(mxDocument, IDocumentEvents_Event) m_activeViewEvents = DirectCast(mxDocument.FocusMap, IActiveViewEvents_Event) AddHandler m_documentEvents.NewDocument, AddressOf NewDocument AddHandler m_documentEvents.OpenDocument, AddressOf OpenDocument AddHandler m_activeViewEvents.ItemAdded, AddressOf ItemAdded AddHandler m_activeViewEvents.ItemDeleted, AddressOf ItemDeleted
Events are unwired then rewired inside the NewDocument and OpenDocument event handlers Private Sub NewDocument() RemoveHandler m_activeViewEvents.ItemAdded, AddressOf ItemAdded RemoveHandler m_activeViewEvents.ItemDeleted, AddressOf ItemDeleted Dim mxDocument As IMxDocument = DirectCast(ESRIContainer.Instance.Application.Document, IMxDocument) m_activeViewEvents = DirectCast(mxDocument.FocusMap, IActiveViewEvents_Event) AddHandler m_activeViewEvents.ItemAdded, AddressOf ItemAdded AddHandler m_activeViewEvents.ItemDeleted, AddressOf ItemDeleted LoadOutputMapItems() End Sub Private Sub OpenDocument() RemoveHandler m_activeViewEvents.ItemAdded, AddressOf ItemAdded RemoveHandler m_activeViewEvents.ItemDeleted, AddressOf ItemDeleted Dim mxDocument As IMxDocument = DirectCast(ESRIContainer.Instance.Application.Document, IMxDocument) m_activeViewEvents = DirectCast(mxDocument.FocusMap, IActiveViewEvents_Event) AddHandler m_activeViewEvents.ItemAdded, AddressOf ItemAdded AddHandler m_activeViewEvents.ItemDeleted, AddressOf ItemDeleted LoadOutputMapItems() End Sub
I didn't include the code for the ItemAdded and ItemDeleted event handlers since it's not important, but these would be the events in my example that would stop firing if I didn't rewire them. Also, technically the extension's Shutdown routine should do a final unwiring of the events but it really doesn't matter since ArcMap is closing at that point.One important thing to note is that the IMxDocument reference is taken from the IApplication reference each time it is needed. If you set a global document reference then you run the risk of it also pointing to the previous document and not the document currently loaded. In my example, I'm using a singleton object class that we use in all of our applications that handles this but you can use the application hook passed to your extension just as easily.