Understanding the Core Objects PackageThe Core Objects package contains the bread and butter of Visual Basic extensibility. In a loose sense, it has the same importance as the MFC (Microsoft Foundation Classes) has in Visual C++. Its objects include the following:
The Root ObjectJust as CObject is the base class of the MFC in Visual C++, so is the VBE (Visual Basic Environment) object to the extensibility services of Visual Basic. The VBE object is also known as the root object of the extensibility model. It's the base object for every extensibility object and collection in Visual Basic. Each object and collection owns a reference to the VBE object via a VBE property. The collections owned by the VBE object include the following:
The VBProjects Collection The VBProjects collection enables you to access a set of Visual Basic properties. This feature can be helpful if your development environment has an established process for developing software; that is, you could easily audit projects quickly to make sure that they're on track. You also could use this facility too rapidly and automatically add enterprise-wide classes or frameworks, or to complete subsystems into a set of projects before a developer adds the first line of new code. Table 31.1 shows some of the more important properties and methods for the VBProjects collection. It also provides brief descriptions of each. Table 31.1. A few key properties and methods of the VBProjects collection |
|
Name | Type | Description |
---|---|---|
Filename | Property | Returns the full pathname of the group project file. |
StartProject | Property | Returns or sets the project that will start when users choose Start from the Run menu, click the Run button, or press the F5 key. |
AddFromFile | Method | Enables you to add or open a project or group project. Its only required argument is the string representing the path of the file you want to add. Also, an optional Boolean argument, if set to True when the file being added isn't a group project file, closes the existing group project; subsequently, the new project is created as the only open project. If it's set to True when the file being added is a group project file, the current group project is replaced by the one you're adding. If it's set to False when the file being added is a group project file, each project in the corresponding group project is inserted into the current group project. The AddFromFile method returns a reference to the VBNewProjects collection. |
AddFromTemplate | Method | Enables you to add project templates into the VBProjects collection. Its only required argument is the string representing the path of the file you want to use as a template. This method is similar to AddFromFile, except that you're dealing with project templates rather than project files. |
The Windows Collection With the Windows collection, you can access windows such as the Project and Properties windows. Also, this collection enables you to access a group of all currently open code windows and designer windows. VB adds a window to this collection each time you open a code or designer window. Similarly, each window you close is removed from this collection. With each window in the collection, for instance, you can show, hide, or position windows. Invoking the Close method has different implementations for different types of windows. For instance, if you invoke Close on a code or designer window in the collection, that window is simply closed and removed from the collection. However, if you invoke it on a window that's a linked window frame, the window isn't closed, but rather unlinked from the other windows. If you invoke the Close method on a Project or Properties window, the object's Visible property is set to False, but it remains in the collection. Table 31.2 shows some of the properties and methods for Window objects in the Windows collection as well as brief descriptions of each: Table 31.2. Some of the properties and methods of Window objects (in the Windows collection) |
|
Name | Type | Description |
---|---|---|
LinkedWindowFrame | Property | A read-only property that returns the Window object representing the frame that contains the window. With this property, you can access the object representing the linked window frame. A linked window frame is a window frame (a listview style area) containing links to more than one window. The Project and Properties windows in VB6 are examples of window frames. The linked window frame has properties distinct from the window(s) it contains. If the window has no links with other windows in the frame, the LinkedWindowFrame property returns Nothing. An example of such a linked window frame would be the Project and Properties windows that are linked by default when you run Visual Basic 6. |
CreateToolWindow | This method creates a new tool window containing a reference to the DocObj object you pass in as the fifth argument. See more about the CreateToolWindow method in the following paragraph. |
The CreateToolWindow method mentioned in
Table 31.1 has the following syntax: object.CreateToolWindow(AddInInst, ProgId, Caption, GuidPosition, DocObj)_As Window The first argument, AddInInst, is an object instance of an add-in from the development environment you pass in. The ProgId argument is of the String data type and represents the program identifier of the ActiveX Document object. The Caption argument is also a String that contains text you want displayed as the caption for the window. The GuidPosition argument is a String that holds a unique identifier for the window. The DocObj argument in the preceding code is an object that represents an ActiveX Document object. When you call this method, the DocObj argument you pass in will be set to an actual ActiveX Document object. Collections of the Windows Collection The Windows collection uses the CodePanes and CommandBars collections. The CodePanes collection enables you to access the collection of code panes now open in your Visual Basic project. The CommandBars collection enables you to access the collection of command bars. Each command bar object is a menu-style toolbar, combining the best of the menus and toolbars that users have come to expect. Each command bar object in the collection can itself contain other command bar objects, given the type of command bar you implement. The different types of command bars are
The VBE object has a CommandBarEvents property that, when accessed, returns the current CommandBarEvents object. Also, the user of your command bar can actually move to another area inside a menu or toolbar. Programmatically speaking, you shouldn't hard-code its actual position. Referencing an object within the CommandBars collection can be straightforward. To reference the Add-Ins menu object, you would declare a command bar object variable and instantiate it as follows: Set objCmdBar = VBInstance.CommandBars("Add-Ins") With this approach, you don't have to memorize the object's ordinal position.
You use the Events object to access properties that enable add-ins to connect to all events in Visual Basic for Applications. The properties of the Events object return objects of the same type as the property name. For example, the CommandBarEvents property returns the CommandBarEvents object. You can use the SelectedVBComponent property to return the active component. (The active component is the component being tracked in the Project window.) If the selected item in the Project window isn't a component, SelectedVBComponent returns Nothing. The IDTExtensibility Interface ObjectThe IDTExtensibility Interface object exposes the public methods and properties of the extensibility model. By exposes, I mean that because you don't directly use the services (methods) and properties of the underlying extensibility model, you need to invoke the methods of the model's agent, so to speak. You can think of interfaces as public agents for the private implementation of an extensibility model object you instantiate. To use the IDTExtensibility object, you have to add a reference to it in your project. To do this, choose Project, References from VB's menu. When the References dialog box appears, find the Microsoft Visual Basic 6.0 Extensibility object and put a check mark next to it. Then click OK. Before you use the IDTExtensibility Interface object, you also need to designate a class to implement it. To implement this interface, insert the following line in the General Declarations section of your class module: Implements IDTExtensibility This line causes a new entry to appear under the Class item in the left-hand drop-down box (object list) of your class module. You'll insert your implementation code in the methods and properties associated with IDTExtensibility. The four methods for Add-In servicing (for managing your Add-Ins) are shown in Table 31.3. These methods define the Add-Ins interface with Visual Basic and are called by the VB IDE at specific times. For example, the IDTExtensibility_OnConnection method is called when VB starts (and the Add-In is part of the current project), when the Add-In is selected via the Add-In Manager, or when the Add-In is selected by the user from VB's Add-In menu. Table 31.3. The four methods that define an Add-Ins interface with Visual Basic |
|
Method Name | Function |
---|---|
IDTExtensibility_OnConnection | Called when the Add-In is being connected to the VB IDE |
IDTExtensibility_OnDisconnection | Called when the Add-In is no longer being used by the VB IDE |
IDTExtensibility_OnStartupComplete | Called when the Add-In has been connected to the VB IDE, though it is only used when the Add-In has been connected when Visual Basic starts |
IDTExtensibility_OnAddInsUpdate | Called whenever the VBADDIN.INI file has been modified |
If you noticed, the methods listed in Table
31.3 have names similar to those of events. When an actual event occurs, these methods
(known as Add-In event handlers) are fired. The class in your code that implements
IDTExtensibility must implement these four event-handling methods, even if that means you
put a Rem comment command or a comment character (') in the interface method.The Visual Basic Instance VariableThe Visual Basic instance variable (also known as a dynamic identification variable) identifies a particular instance of your Visual Basic session. This instance identifier enables you to have separately identifiable running instances of Visual Basic in memory. The instance variable is of the type VBIDE.VBE. To use this variable, declare it in a class module or general module (also known as a class utility in MS Visual Modeler, a software design tool now available for Visual Basic 6). Therefore, if you declared it in a class module as private, the declaration would look like the following: Private mVBInst As VBIDE.VBE The prefix m identifies the variable as a module-level variable.
|
|
![]() |
![]() |
|||