Visual Basic Concepts
Programming in the Extensibility Model
Once you have a framework in place for your add-in, you can begin to attach code to manipulate and/or extend Visual Basic. As mentioned in Chapter 2, "Extensibility Model Objects and Structure," the extensibility model can be broken down into a number of functional groups, each affecting a major portion of Visual Basic.
Note Many of the example code fragments below are included to demonstrate how the various collections and objects, and their properties, methods, and events, are used in a real-world program. As such, they may contain references to variables, controls, and such that aren't fully explained in this text. For example, many of the samples reference a variable named "vbi", which is a variable to reference the current instance of Visual Basic. (For more information on this, see "Visual Basic Instance Variable" in "Add-In Essentials".) The important thing to realize is that these examples should act as a basis for your own exploration of the extensibility model, not as complete, free-standing programs. To gain a good feel for the entire feature set, we recommend that you examine the VBIDE library in the Object Browser. We also urge you to examine the TabOrder (which is listed in the directory) for more examples of programming add-ins.
The following topics cover most functional groups of objects in the extensibility model, and provide suggestions for using each group to achieve your goals:
Manipulating the IDE with Add-Ins These objects and collections allow you to select, view, and alter visual elements of the Visual Basic IDE, such as windows, CommandBars, and CodePanes.
Manipulating Projects with Add-Ins This set of objects allows you to monitor, select, and alter project elements in the Visual Basic IDE, which are typically displayed in the Project Explorer window.
Manipulating Forms and Controls with Add-Ins This set of objects gives you control over forms and the controls that reside on them.
Responding to Events with Add-Ins With Visual Basic, you're given a new, higher level of control over events; using this group of objects, you can respond to events that occur in the IDE itself, such as when a user clicks a form or project name in the Project Explorer window.
Manipulating Code with Add-Ins Using this set of objects, you can take control of source code in any object. Find out information about the code, such as what the starting or ending line is, how many lines of code exist in a module, or even delete or insert lines of code.
Manipulating Add-Ins The Add-Ins collection gives you information about other add-ins on the system. This allows you to make better decisions about how to run a particular add-in and make it respond differently, or you can use other existing add-ins for your own purposes.
Note When using C++ to develop add-ins, whenever you get a pointer from Visual Basic using Automation, you must call "release" when you are finished using it. Visual Basic follows the standard reference counting rules. For example, when you get a pointer out of Visual Basic by calling _NewEnum, you must call release through that pointer. Also, when you use pDispatch = m_ourMenuItems.Item(pszCaption)
, you must also use pDispatch->Release()
.