Creating a COM Add-In

You can create a COM add-in for Office 2000 in any programming language that lets you create a COM .dll or COM .exe file. These languages include Microsoft Visual C++, Visual J++, Visual Basic version 5.0 or higher (Professional or Enterprise Edition), and the version of Visual Basic for Applications included in Microsoft Office 2000 Developer. The examples in this chapter use Visual Basic version 6.0.

Creating a COM Add-In with Visual Basic

To create a simple COM add-in for Office 2000 using Visual Basic version 6.0, complete the following steps:

  1. Start Visual Basic. In the New tab on the New Project dialog box, double-click the ActiveX DLL icon to create a new project. The Class1 class module is added by default.
  2. In the Project Explorer, right-click the Class1 class module and select Remove Class1. Click No when prompted to save changes in Class1.
  3. From the Project menu, click Components to display the Components dialog box for the current project.
  4. Click the Designers tab and select Addin Class (if it's not already selected). This adds the menu item Add Addin Class to the Project menu.
  5. Click to view at full size.

  6. Click Add Addin Class from the Project menu. In the Properties window, change the Name property of the Designer project item from AddinDesigner1 to ConnectWord. Set the Public property to True and then click OK.
  7. In the General tab of the ConnectWord window, enter or select the values listed in the following table. You may have to resize the Add-in Designer window in order to see the Initial Load Behavior drop-down list at the bottom of the General tab.
  8. Property Value
    Addin Display Name text box Type Microsoft Office Basic COM add-in for Word.
    Application drop-down list Microsoft Word
    Application Version drop-down list Microsoft Word 9.0
    Initial Load Behavior drop-down list None

    The COM add-ins dialog box displays the "friendly name" of the add-in, which is "Microsoft Office Basic COM add-in for Word." The Application drop-down list contains the applications for which you can register your COM add-in. COM add-ins are supported in Office 2000 and later versions. If you have Office 2000 installed on your machine, each Office 2000 application will be listed. The Initial Load Behavior drop-down list at the bottom of the General tab allows you to set how your add-in loads. Chapter 14, in the section "COM Add-in Load Behavior Settings," describes the different load behaviors in more detail.

    The default load behavior in the General tab is set to None; consequently, the add-in won't automatically load when you start Word. Instead, you have to load the add-in using the COM Add-Ins dialog box. The next section, "Test the COM Add-in," describes how to use the COM Add-Ins dialog box.

  9. In the Project Explorer window, make sure you select the ConnectWord project item, and then click the View Code button or press F7. In the ConnectWord code window, select AddinInstance from the Object drop-down list. When you select AddinInstance, the Sub procedure AddinInstance_OnConnection is automatically inserted, as shown in the figure below.
  10. Click to view at full size.

  11. Add the following line to the AddinInstance_OnConnection procedure:
  12. MsgBox "Add-in connected to: " & Application.Name
    

  13. Select OnDisconnection from the Procedures drop-down list and add the following line to the AddinInstance_OnDisconnection procedure:
  14. MsgBox "Add-in disconnected"
    

  15. From the Project menu, click Project1 Properties. Type BasicCOMAddIn in the Project Name text box and then click OK.
  16. Click to view at full size.

    The project name represents the first part of the programmatic identifier (ProgID) of your COM add-in. The ProgID lets you uniquely identify your COM add-in.

NOTE
Programmatic identifiers are described in the next chapter.

  1. On the File menu, click Save Project and save the project files under the names shown in the following table. Note that Visual Basic assigns the appropriate extension to each file.
  2. File File name Extension
    Add-in Designer ConnectWord .dsr
    Project BasicCOMAddIn .vbp

Test the COM Add-In

The next step is for you to test your COM add-in.

  1. Click the File menu, and then click Make BasicCOMAddIn.dll. In the Make Project dialog box, click OK.
  2. When you compile the .dll, it's automatically registered in the Windows system registry. Because of the Add-in Designer, the add-in is registered where the Office application (such as Word) will know to load it.

  3. Start Word. Click Customize on the Tools menu to display the Customize dialog box. Then click the Commands tab and select Tools from the Categories list box. Scroll down the Commands list box adjacent to the Categories list box until the COM Add-Ins command is in view.
  4. In Word, the COM Add-Ins command is listed at the top of the Commands list box. However, the COM Add-Ins command is not listed in the same position in the Commands list box of every Office application, so you'll then need to scroll down the list.

  5. Click COM Add-Ins and drag the command to the right end of the menu bar in Word, beside the Help menu.
  6. Click the COM Add-Ins command to open the COM Add-Ins dialog box. Select the Microsoft Office Basic COM Add-In for Word check box, and then click OK.
  7. The following message box appears. Click OK to dismiss it.

  8. Click COM Add-Ins again. Clear the Microsoft Office Basic COM Add-In for Word check box, and then click OK. Visual Basic displays a message box with the text, "Add-in disconnected." After you click OK on the message box, the add-in is disconnected.

The COM add-in you created is now compiled as a .dll file, registered, and available on your computer. When your add-in is fully developed and debugged, you'll need to package and deploy it—that is, make it available to other users. The last section of this chapter describes how to deploy your COM add-in so that you can register and install it on any user's machine.