Microsoft Office 2000/Visual Basic Programmer's Guide   

Working with Host Application Object Models

There are a few things to keep in mind as you add forms and other components to your COM add-in. First of all, your COM add-in is like a separate application running inside an Office 2000 application. Therefore, you need to set references to any object libraries you want to work with from within the COM add-in's project. If your add-in will be run in more than one application, you can use the OnConnection event procedure to determine which application your add-in is currently running in and then selectively run code that works with that application's objects.

To figure out in which application the add-in is currently running, use the object supplied by the Application argument of the OnConnection event procedure. Assign this object variable to a global object variable. In the code that interacts with the host application, check to see which application you're working with, and use that application's object model to perform the task.

A DLL is loaded into memory only once, but each application that accesses the DLL gets its own copy of the DLL's data, stored in a separate space in memory. Therefore, you can use global variables in a COM add-in without worrying about data being shared between two applications that are using the COM add-in at the same time. For example, the Image Gallery sample add-in can run simultaneously in Word, Excel, and PowerPoint. When Word loads the add-in, the OnConnection event occurs and a reference to the Word Application object is stored in a global variable of type Object. If Excel then loads the add-in, the OnConnection event occurs and a reference to the Excel Application object is also stored in a global variable of type Object, but in a different space in memory. Within the code for the add-in, you can use the If TypeOf…End If construct to check which application's Application object the variable points to. The Click event procedure for the cmdInsert button in frmImageGallery in the Image Gallery project, located in the ODETools\V9\Samples\OPG\Samples\CH11\ImageGallery subfolder on the Office 2000 Developer CD-ROM, uses this approach:

' Global object variable, declared in modSharedCode module.
Public gobjAppInstance As Object

Private Sub cmdInsert_Click()
   ' Insert selected image.
   ' Check which object variable has been initialized.
   If TypeOf gobjAppInstance Is Word.Application Then
      ' Insert into Word.
      Word.Selection.InlineShapes.AddPicture FileName:= _
         img(mlngSel).Tag, LinkToFile:=False, _
         SaveWithDocument:=True
   ElseIf TypeOf gobjAppInstance Is Excel.Application Then
      gobjAppInstance.ActiveSheet.Pictures.Insert img(mlngSel).Tag
   ElseIf TypeOf gobjAppInstance Is Powerpoint.Application Then
      gobjAppInstance.ActiveWindow.Selection.SlideRange.Shapes.AddPicture _
         FileName:=img(mlngSel).Tag, LinkToFile:=msoFalse, _
         SaveWithDocument:=msoCTrue, Left:=100, Top:=100
   End If 
End Sub