本帖最后由 liucqa 于 2013-8-30 19:31 编辑
http://msdn.microsoft.com/en-us/library/office/aa189036(v=office.10).aspx
Building COM Add-ins for the Visual Basic EditorOffice 2000
This topic has not yet been rated - Rate this topic
By creating COM add-ins for the Visual Basic Editor, you can customize your development environment and work with components in a VBA project from code. For example, you can build a code wizard that walks a programmer through a series of steps and then builds a procedure. Or you can build a code analyzer that determines how many times and from where a procedure is called. Creating COM add-ins for the Visual Basic Editor is one way to make your own job easier! When you create a COM add-in for the Visual Basic Editor, it appears in all instances of the Visual Basic Editor. You can't, for example, create a COM add-in that appears only in the Visual Basic Editor in Word; it will also appear in the Visual Basic Editor in Access, Excel, PowerPoint, FrontPage, and any other VBA host applications on the computer where the COM add-in DLL is registered. Note also that you can create multiple add-ins in a single DLL. Each add-in designer in the add-in project represents a separate add-in. For example, you can create a single DLL that contains a suite of add-ins for developers, and the developers can load just the add-ins they want to use. To control the Visual Basic Editor from the code inside an add-in, you use the Microsoft Visual Basic for Applications Extensibility 5.3 library. This object library contains objects that represent the parts of a VBA project, such as the VBProject object and the VBComponent object. The top-level object in the VBA Extensibility library object model is the VBE object, which represents the Visual Basic Editor itself. For more information about this object library, use context-sensitive Help (F1) in the Object Browser. For a diagram of its objects, see the http://msdn.microsoft.com/en-us/library/office/cc326919.aspx . Note Don't confuse the VBA Extensibility library with the IDTExtensibility2 library. Although their names are similar, the VBA Extensibility library provides objects that you can use to work with the Visual Basic Editor from an add-in while it is running, and the IDTExtensibility2 library provides events that are triggered when the add-in is connected or disconnected. In addition, don't confuse the VBA Extensibility library with the Microsoft Visual Basic 6.0 Extensibility library, which is used for creating add-ins in Microsoft Visual Basic. To see a sample COM add-in for the Visual Basic Editor, copy the DevTools project from the ODETools\V9\Samples\OPG\Samples\CH11\DevTools subfolder on the Office 2000 Developer CD-ROM to your computer. The DevTools sample project includes two simple add-ins that work with objects in the VBA Extensibility library. The Insert Procedure Template add-in found in this project inserts a new procedure skeleton into a selected code module, complete with scoping, arguments, a return value, and simple error handling. The PathFinder add-in saves you from having to type long file paths — you can use it to navigate to the file you're interested in, and then copy a string containing the file path.
http://msdn.microsoft.com/en-us/library/office/aa164903(v=office.10).aspx
Creating a COM Add-in for the Visual Basic Editor
Office 2000 0 out of 1 rated this helpful - Rate this topic
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
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.
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.
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.
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.
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.
Build any forms or other components to be included in the project.
Place a breakpoint in the OnConnection event procedure, and then click Start with Full Compile on the Run menu.
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.
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."
|