Using Microsoft.Vbe.Interop
in C#, I can access CommandBarEvents
and ReferencesEvents
via VBE.Events
.
However the ever-so helpful MSDN documentation seems to indicate that there's a VBProjectsEvents
that I could use to notify my add-in when a project is added or removed to/from the VBE... which is exactly what I'm trying to achieve here.
I can see that _VBProjectsEvents
interface in the object browser, but no implementation for it (as opposed to the _CommandBarControlsEvents
interface, which is implemented by the CommandBarEventsClass
), using ReSharper's go to implementation feature.
Is there an implementation of the _VBProjectsEvents
interface anywhere? If not, then how does one go about being notified of a VBProject being removed from the IDE?
You need to create a sink for these events.
Implement the
_dispVBProjectsEvents
dispatch interface - here's an implementation that responds to these events by invoking regular managed .net events, effectively "wrapping" theVBProjects
events:The
DispatcherEventArgs
class is just a convenient way to expose theVBProject
item involved with the event, and it can be reused for other sinks:The client code needs to register the sink - and for that you need to keep an
IConnectionPoint
field and itsint
cookie:The
VBProjects
collection implements theIConnectionPointContainer
interface, which you need to use to find the connection point:Once you have the
IConnectionPoint
, use theAdvise
method to "connect" your sink and retrieve a cookie:And then you can handle the sink events as you would any "normal" .net events:
When you want to disconnect your sink, you pass the cookie to the
Unadvise
method of theIConnectionPoint
instance:"Simple as that!"