The key to understanding COM add-in development is the IDTExtensibility2 interface. This interface is used by all Office applications to communicate with a COM add-in. This ensures a common initialization mechanism and an ability to pass in the application's object model so that the COM add-in can communicate with the Office application. Listing 23.1 shows the IDTExtensibility2 interface. Listing 23.1. The IDTExtensibility2 Interface
Startup OrderIDTExtensibility2 is a simple interface, but it is important to note the loading order of the COM add-in and how that affects where you write your code. Office instantiates your COM add-in, which causes your main Connect class to be created. But there is a key difference from normal programming practice, in that the constructor of your Connect class cannot be used to set up your class, because the Office application context (typically, the Application object from the Office application's object model) is not made available in the constructor. Instead, it is provided via the OnConnection method on the IDTExtensibility2 interface. Likewise, the shutdown behavior for an add-in is determined not by the destructor of the class, but by when the OnDisconnection method is called. Figure 23.5 illustrates the order in which these events occur for a COM add-in. First, the COM add-in is loaded, and the Connect class is created. This results in the Connect class's constructor being called. Then the Connect class's implementation of IDTExtensibility2.OnDisconnection is called, and the Office application's Application object is passed via this method. The Connect class's implementation of IDTExtensibility2.OnStartupComplete is called. Now the add-in is loaded and connected. Then, when the application exits or the user unloads the add-in, the Connect class's implementation of IDTExtensibility2.OnBeginShutdown is called, followed by a call to IDTExtensibilty2.OnDisconnection. Figure 23.5. Order of COM add-in startup and shutdown.
The OnAddInsUpdate MethodThe OnAddInsUpdate method is called when any COM add-in is loaded or unloaded in the Office application. This method is somewhat of an anomaly because the contents of the custom argument are never set by Office applications. As a result, this method really can be used only to tell you that a COM add-in has been loaded or unloaded; then you can query the COMAddins collection in the application object model to see what has been loaded or unloaded. A good example of using this method is if your COM add-in relies on other COM add-ins to be running to work properly. So if one of the dependent COM add-ins is unloaded, your COM add-in can unload this: Sub OnAddInsUpdate(ByRef custom As Array)
The OnBeginShutdown MethodThe OnBeginShutdown method is called on a connected COM add-in when the Office application is being shut down: Sub OnBeginShutdown(ByRef custom As Array)
The OnConnection MethodThe OnConnection method is called when a COM add-in is loaded into the environment. This method is the main entry point for the COM add-in, because it provides the Application object from the Office application's object model that the add-in will use to communicate with the Office application: Sub OnConnection(ByVal Application As Object, _ ByVal ConnectMode As ext_ConnectMode, _ ByVal AddInInst As Object, ByRef custom As Array)
The OnDisconnection MethodThe OnDisconnection method is called when a COM add-in is unloaded from the application, either because the application is shutting down or because the user disabled the COM add-in using the COM AddIns dialog box: Sub OnDisconnection(ByVal RemoveMode As ext_DisconnectMode, _ ByRef custom As Array)
The OnStartupComplete MethodThe OnStartupComplete method is called when the Office application has completed starting up and has loaded all the COM add-ins that were registered to load on startup: Sub OnStartupComplete(ByRef custom As Array)
A Simple Implementation of IDTExtensibility2Listing 23.2 shows a simple implementation of IDTExtensibility2 similar to what is generated when you create an add-in project in Visual Studio. This implementation displays several message boxes to give you information about the methods of IDTExtensibility2 that are being called on the Connect class. It is a COM add-in that loads into Excel, so it casts the application object to the Microsoft.Office.Interop.Excel.Application type. It also casts the addInInst object to the Microsoft.Office.Core.COMAddin type. Note also that the InteropServices namespace is used to add a GuidAttribute and ProgID attribute. The values of these attributes are used when registering the add-in, as described earlier in the chapter. Listing 23.2. An Excel COM AddIn Connect Class That Implements IDTExtensibility2
|