Microsoft Office 2000/Visual Basic Programmer's Guide |
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
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.
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."