Working with the Microsoft Visual Basic for Applications Extensibility Library

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

The Microsoft® Visual Basic® for Applications (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 is 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 are currently working with.

For example, suppose you have a variable named vbeCurrent, of type VBIDE.VBE, which 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."

See Also

Creating a COM Add-in for the Visual Basic Editor