Microsoft Office 2000/Visual Basic Programmer's Guide   

COM Add-ins and Security

You can specify security settings for Office 2000 applications in the Office 2000 Security dialog box, available by pointing to Macro on the Tools menu and then clicking Security. The Security Level tab includes a check box, Trust all installed add-ins and templates. If this box is selected, Office 2000 applications will load all COM add-ins, application-specific add-ins, and templates in trusted folders without checking to see whether they have valid digital signatures from trusted sources. For more information about trusted folders, see Chapter 2, "Designing and Deploying Office Solutions," and Chapter 17, "Securing Office Documents and Visual Basic for Applications Code."

If this check box is not selected, the Office 2000 application checks to see whether the add-in or template has been digitally signed by a trusted source before loading it. If it has, the add-in will be loaded under any security level. If it has not been signed, if it has not been signed by a trusted source, or if the signature has been invalidated, the add-in will not load under high security. Under medium security, users will be warned that the add-in may not be safe. Under low security, the add-in will load and run without prompting the user.

To digitally sign a COM add-in DLL, you need to obtain a digital certificate from a certificate authority, and you need to run the Signcode.exe utility included with the Microsoft Internet Client Software Development Kit (SDK) on the COM add-in DLL. A digital certificate identifies the developer of a component as a trusted source. For more information about digitally signing a DLL, search the Microsoft Developer Network Web site, at http://msdn.microsoft.com/developer/, for "digital signing." For more information about security for VBA projects and digital signatures, including how to obtain a digital certificate and the Internet Client SDK, see Chapter 17, "Securing Office Documents and Visual Basic for Applications Code."

You can use the COMAddIn object and the COMAddIns collection to control COM add-ins from VBA code that is running within the host application. For example, you can load an add-in programmatically when a user clicks a button to access a particular feature. Or, you can load an add-in from VBA when you open an application through Automation (formerly called OLE Automation).

The Microsoft Office 9.0 object library supplies the COMAddIn object and the COMAddIns collection. The Application object for each Office 2000 application — Word, Excel, PowerPoint, Access, FrontPage, and Outlook — has a COMAddIns property, which returns a reference to the COMAddIns collection. For any application, the COMAddIns collection contains only those COM add-ins that are registered for that application. The COMAddIns collection in Excel, for example, contains no information about COMAddIn objects in Word.

The Connect property of a COMAddIn object sets or returns the load status of the add-in. Setting this property to True loads the add-in, while setting it to False unloads it.

The ProgId property returns the name of the registry subkey that stores information about the COM add-in. The registry subkey takes it name from the COM add-in's programmatic identifier, which consists of the name of the add-in project followed by the name of the add-in designer or class module that's actually supplying the add-in for a particular application. For example, when it's properly registered, the Image Gallery sample add-in for Excel has the following value for its ProgId property:

ImageGallery.dsrImageExcel

The name of the add-in project is ImageGallery, and the name of the add-in designer for the Excel version of the add-in is dsrImageExcel.

You can use an add-in's ProgId property value to return a reference to the add-in from the COMAddIns collection, as shown in the following code fragment, which prints the current value of the Excel Image Gallery COM add-in's Connect property:

Debug.Print Excel.Application.COMAddIns("ImageGallery.dsrImageExcel").Connect

You can use the COMAddIn object and COMAddIns collection to get information about available COM add-ins from code running in an Office 2000 application. You can also use it to load and unload add-ins from code running in the add-in host application, or from code that is performing an Automation operation on the host application from another application.

If you're concerned about the performance of your application, you may want to load an add-in only at certain times. You can control this by loading and unloading it through VBA code.

The following code uses Automation to launch Word from another application, such as Excel, and load the Image Gallery add-in. To run this code from another application, remember to first set a reference to the Word object library.

Function LoadWordWithImageGallery() As Boolean
   ' Loads Word and connects Image Gallery add-in.
   ' If Image Gallery add-in is not available, procedure
   ' fails silently and returns False.
   
   Dim wdApp         As Word.Application
   Dim comAddIn      As Office.comAddIn
   
   ' Create instance of Word and make visible.
   Set wdApp = New Word.Application
   wdApp.Visible = True
   ' Add new document.
   wdApp.Documents.Add
   
   ' Return reference to COM add-in, checking for error
   ' in case it doesn't exist.
   On Error Resume Next
   ' Set reference to COM add-in by using its ProgId property value.
   Set comAddIn = wdApp.ComAddIns("ImageGallery.dsrImageWord")
   If Err.Number = 0 Then
      ' Connect add-in.
      comAddIn.Connect = True
      ' Perform other operations here.
      ' .
      ' .
      ' .
      LoadWordWithAddIn = True
   Else
      ' Return False if error occurred.
      LoadWordWithAddIn = False
   End If
   
   ' Enter break mode here to verify that add-in is loaded.
   Stop
   
   ' Quit Word.
   wdApp.Quit
   Set wdApp = Nothing
End Function

The LoadWordWithImageGallery procedure is available in the modAddInObjects module in AddInObjects.xls in the ODETools\V9\Samples\OPG\Samples\CH11 subfolder on the Office 2000 Developer CD-ROM.

For more information about the COMAddIn object and the COMAddIns collection, search the Microsoft Office Visual Basic Reference Help index for "COMAddIn object" and "COMAddIns collection."