Microsoft Office 2000/Visual Basic Programmer's Guide   

Creating a COM Add-in for the Visual Basic Editor

For the most part, creating a COM add-in for the Visual Basic Editor is similar to creating one for an Office 2000 application. COM add-ins for the Visual Basic Editor also include either the add-in designer or a class module that implements the IDTExtensibility2 library. You can begin with the COM add-in template project.

One key difference to note is that the initial load behavior setting for a COM add-in for the Visual Basic Editor differs from that of a COM add-in for an Office application. A COM add-in for the Visual Basic Editor can have one of two initial load behaviors: None, meaning that the add-in is not loaded until the user loads it, or Startup, meaning that the add-in is loaded when the user opens the Visual Basic Editor.

To create a COM add-in in Visual Basic 6.0 for the Visual Basic Editor

  1. Create a new COM add-in project and modify the add-in designer so that the Application box is set to VBE and the Application Version box is set to VBE 6.0. Set the initial load behavior for the add-in to either None or Startup.

  2. Set a reference to the Microsoft Visual Basic for Applications Extensibility 5.3 library. The file Vbe6ext.olb contains this object library; if the object library doesn't appear in the list of available references, it is installed by default in C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6. The name of the library as it appears in the Object Browser is VBIDE.

  3. In the add-in designer's class module, implement the IDTExtensibility2 library as described in "Implementing the IDTExtensibility2 Library" earlier in this chapter. Make sure that each event procedure contains code or a comment.

  4. The OnConnection event procedure passes in the Application argument, which contains a reference to the instance of the Visual Basic Editor in which the add-in is running. You can use this object to work with all other objects in the VBA Extensibility library. To do so, create a public module-level object variable of type VBIDE.VBE, and assign the object referenced by the Application argument to this variable.

  5. Within the OnConnection event procedure, you can optionally include code to hook the add-in's form up to a command bar control in the Visual Basic Editor. You can work with the Visual Basic Editor's command bars by using the CommandBars property of the VBE object.

  6. Build any forms or other components to be included in the project.

  7. Place a breakpoint in the OnConnection event procedure, and then click Start with Full Compile on the Run menu.

  8. In a VBA host application, such as Excel, open the Visual Basic Editor, click Add-in Manager on the Add-ins menu, and select your add-in from the list. Select the Loaded/Unloaded check box to load the add-in, if it's not set to load on startup.

  9. Debug the add-in. When you've debugged it to your satisfaction, end the running project in Visual Basic 6.0, and make the add-in's DLL by clicking Make projectname.dll on the File menu. 

You can use the same strategies to distribute COM add-ins for the Visual Basic Editor as you use to distribute COM add-ins for the Office 2000 applications. For more information, see "Distributing COM Add-ins" earlier in this chapter.

Working with the Microsoft Visual Basic for Applications Extensibility 5.3 Library

The VBA extensibility library provides objects that you can use to work with the Visual Basic Editor and any VBA projects that it contains. From an add-in created in Visual Basic 6.0, you can return a reference to the VBE object, the top-level object in the VBA Extensibility library, through the Application argument of the OnConnection event procedure. This argument provides a reference to the instance of the Visual Basic Editor in which the add-in is running.

The VBProject object refers to a VBA project that's open in the Visual Basic Editor. A VBProject object has a VBComponents collection, which in turn contains VBComponent objects. A VBComponent object represents a component in the project, such as a standard module, class module, or form. Because a VBComponent object can represent any of these objects, you can use its Type property to determine which type of module you're currently working with.

For example, suppose you have a variable named vbeCurrent, of type VBIDE.VBE, that represents the instance of the Visual Basic Editor in which the add-in will run. The following code fragment prints the names and types of all components in the active project to the Immediate window:

Dim vbcComp As VBIDE.VBComponent

For Each vbcComp In vbeCurrent.ActiveVBProject.VBComponents
   Debug.Print vbcComp.Name, vbcComp.Type
Next vbcComp

A VBComponent object has a CodeModule property that returns a CodeModule object, which refers to the code module associated with that component. You can use the methods and properties of the CodeModule object to manipulate the code in that module on a line-by-line basis. For example, you can insert lines by using the InsertLines method, or perform find and replace operations by using the Find and Replace methods.

To work with command bars in the Visual Basic Editor, use the CommandBars property of the VBE object to return a reference to the CommandBars collection.

For more information about working with the VBA Extensibility library, search the Visual Basic Reference Help index for "VBProject object."