Visual Basic 4.0 introduces add-ins. Add-ins are to the Visual Basic programming environment what custom controls are to your applications: pieces of compiled functionality that you can simply plug in and use. Unlike custom controls, however, add-ins can be created by the programmer right in Visual Basic. This chapter explains how to create add-ins in Visual Basic.
In this chapter, you learn about the following:
The Visual Basic Add-In Manager loads add-in files into the Visual Basic programming environment. Once loaded, an add-in can extend Visual Basic in three ways: by providing new items on the Add-Ins menu; by providing custom templates for new forms; or by adding special features, such as automatic source control.
You can create add-ins using Visual Basic Professional or Enterprise Editions. Add-ins created in Visual Basic include special methods in a public, creatable class. When you load an add-in, Visual Basic starts the add-in's .EXE file and connects it to specific Visual Basic events. Classes in the add-in's executable can respond to these events and interact with Visual Basic through objects that the Visual Basic programming environment provides.
The add-in's executable file stays loaded in memory until Visual Basic quits or the user unloads the add-in from the Add-In Manager. During this time, the add-in can respond to certain user events as they occur in Visual Basic. These events include saving files, adding new forms, and clicking on certain menu items. Figure 24.1 shows how an add-in interacts with Visual Basic.
Once loaded, add-ins can use Visual Basic add-in objects and respond to events in the programming environment.
To load an add-in in Visual Basic, follow these steps:
The Add-In Manager displays the available add-ins.
After you select add-ins in the Add-In Manager, they load each time that you start Visual Basic. The [add-ins16] or [add-ins32] section of the VB.INI file stores these selections, as follows:
The name of the add-in (RegEditAddin.Application) shown in the preceding example is the programmatic ID of the add-in as it is registered in the system registration database. The setting for each add-in (0 or 1) indicates whether the add-in is loaded.
An add-in might be registered in the system registration database, but if the add-in is not listed in the add-ins section of VB.INI, it does not appear in the Add-In Manager.
Visual Basic provides an object library, VBEXT.OLB, that you can use to extend the Visual Basic programming environment. Figure 24.3 shows the hierarchy of the objects that Visual Basic provides. Table 24.1 describes the tasks that you perform with each of the objects.
The Visual Basic add-in object hierarchy.
Table 24.1 VBEXT Objects by Task
Task | Object | Description |
---|---|---|
Load an add-in | AddInManager | Triggers ConnectAddin and DisconnectAddin methods in your add-in. You use these events to activate and deactivate your add-in when loaded in Visual Basic. |
Application | Provides access to all the subordinate objects. You get a reference to the Application object from the ConnectAddin method in your add-in. | |
Add items to the Add-Ins menu | Menu | Controls the display of items in the Add-Ins menu. |
MenuItems | Adds or removes menu items or menus from the Add-Ins menu. | |
MenuLine | Controls the text displayed for a menu item in the Add-Ins menu. Also defines the action taken when the user clicks the menu item. | |
Control the active project | ProjectTemplate | Controls the active project by providing access to all the subordinate objects. |
SelectedComponents | Provides access to all the form, class, and code modules in the active project. | |
Component | Controls a form, class, or code module. | |
FormTemplate | Provides access to the controls on a form. | |
ControlTemplates | Adds new controls to a form; returns ControlTemplate objects for each of the controls on a form. | |
ControlTemplate | Provides access to the Properties collection of a control on a form. | |
Properties | Provides access to each of a control's property settings. | |
Property | Sets or returns the setting of a control's property. | |
Respond to File menu events | FileControl | Triggers methods in your add-in when the user chooses any of the items from the Visual Basic File menu. |
Three add-in objects expose events that you can detect:
Table 24.2 lists the add-in object events and describes when each occurs.
Table 24.2 Add-In Object Events
Object | Event | Trigger |
---|---|---|
AddInManager | ConnectAddIn | Visual Basic loads the add-in. |
DisConnectAddIn | Visual Basic unloads the add-in or Visual Basic quits. | |
MenuLine | AfterClick | The user clicks on the menu item. |
FileControl | AfterAddFile | The user adds a form, code, class, resource, or .VBX file to a project. |
AfterChangeFileName | Visual Basic saves a file, renames a file, or creates an .EXE file. | |
AfterCloseFile | The project file closes. This includes a project file closing because Visual Basic is quitting. | |
AfterRemoveFile | The user removes a form, code, class, resource, or .VBX file from a project. | |
AfterWriteFile | A project, form, code, or class file is written to disk. | |
BeforeLoadFile | Visual Basic loads a project form, code, class, resource, .FRX, or .VBX file. | |
DoGetAddFileName | The user chooses File, Add. | |
DoGetNewFileName | The user chooses File, Save File or File, Save Project for a new file; or the user chooses the File menu's Save File As, Save Project As, or Make EXE File for an existing file. | |
DoOpenProjectName | The user chooses File, Open Project. | |
RequestChangeFileName | Visual Basic prepares to rename a project, form, class, or code module file to disk. | |
RequestWriteFile | Visual Basic prepares to save a project, form, class, or code module file to disk. |
The AddInManager object automatically associates two, special method names with its events: ConnectAddIn and DisconnectAddIn. If you provide methods with these names in a public, creatable class, the Add-In Manager creates an instance of that object and invokes the ConnectAddIn method when the add-in is loaded. The ConnectAddIn method has the following form:
The Application argument is a passed-in reference to the current instance of the Visual Basic Application object. This reference is your one opportunity to access the Application object, so you must use this reference to derive all subsequent object references in your add-in.
The Add-In Manager invokes the DisconnectAddIn method just before it unloads the add-in. The add-in might unload if a user deselects the add-in in the Add-In Manager or if Visual Basic quits. The DisconnectAddIn method has the following form:
The Mode argument indicates why the add-in is unloading: 0 indicates that Visual Basic is quitting, and 1 indicates that the user deselected the add-in in the Add-In Manager. Use the DisconnectAddIn method to unload any forms that your add-in uses and to save any settings that you want to preserve.
To detect the events for MenuLine or FileControl objects, you must create an association between the event and the object that you've created to respond to the event. The add-in object's ConnectEvents method creates this association. The following code shows how to use the ConnectAddIn event to add a line to the Add-Ins menu. The code associates the new line with the AfterClick method in this object.
The object that you associate with an event must exist before you call ConnectEvents. The following code shows how to create an instance of an object before associating the object with a FileControl event:
Listing 24.1 shows the class definition for the clsFileControl object. This object simply displays a message box indicating when a file event occurs in Visual Basic. You can use this class as a template for your own file control object.
Listing 24.1 The Class Definition for the clsFileControl Object
The add-in objects don't closely follow the OLE naming conventions for collections. Therefore, you sometimes must use nonobvious method and property names when navigating down the add-in object hierarchy. Table 24.3 shows the properties and methods that you use to navigate down each level in the add-in object hierarchy.
Table 24.3 Properties and Methods Used to Navigate down the Add-In Object Hierarchy
From Object | To Object | Use This Property/Method |
AddInManager | Application | None; an Application object is passed in as the argument to the ConnectAddin method |
Application | Menu | AddInMenu property |
Menu | MenuItems | MenuItems method |
MenuItems | MenuLine | Item method (default method) |
MenuItems | Menu | Item method (default method) |
Application | ProjectTemplate | ActiveProject property |
ProjectTemplate | SelectedComponents | SelectedComponents property |
SelectedComponents | Component | ActiveForm property or
Item method (default method) |
Component | ControlTemplates | ControlTemplates method orSelectedControlTemplates property |
ControlTemplates | ControlTemplate | Item method (default method) |
ControlTemplate | Properties | Properties method |
Properties | Property | Item method (default method) |
Application | FileControl | FileControl property |
To navigate up the add-in object hierarchy, use the Parent or Application properties. All add-in objects provide these properties.
To create an add-in, write code to perform the following actions:
The following sections explain each of these steps in greater detail.
Add-ins must have at least one class with Creatable and Public properties set to True. Typically this class is called Application and provides the programmatic ID that you install in the VB.INI file. Visual Basic doesn't provide a built-in way to add entries to the .INI files, so you must use the Windows API function WritePrivateProfileString to perform this task.
Listing 24.2 shows a Main procedure that installs the add-in in the VB.INI file if you run the add-in .EXE file with the /install switch. This procedure demonstrates a handy way to distribute your add-ins, although it increases the code size slightly. You can use this Main procedure in your own add-ins simply by modifying the module-level constants and making it the startup procedure for your add-in.
Listing 24.2 Registering an Add-In with Visual Basic.
The Application class that provides the programmatic ID that you install in the VB.INI file must contain the ConnectAddIn and DisconnectAddIn methods. These methods initialize the add-in when it is loaded, and deinitialize it when it is unloaded or when Visual Basic quits.
The ConnectAddIn has one argument, Application, which passes in a reference to the instance of Visual Basic that is loading the add-in. You use this object reference in all subsequent access to Visual Basic objects, so you must preserve its value before the ConnectAddIn procedure ends and the argument loses scope. The best way to save the value of Application is as the Parent property of the add-in's Application object. This is consistent with the OLE standard for object navigation and enables you to get the Visual Basic Application object from all the other objects in your add-in.
Listing 24.3 shows the ConnectAddIn method for the Application class of the REGADDIN.VBP sample, a simple add-in that adds the Registration Info Editor to the Visual Basic Add-Ins menu.
Listing 24.3 The ConnectAddIn Method for REGADDIN.VBP's Application Class
The Parent property of the Application class is a write-once, read-always property. After initializing Parent, the ConnectAddIn method is effectively read-only. Listing 24.4 shows the definition for the Parent property of the Application class in the REGADDIN.VBP sample.
Listing 24.4 The Parent Property for REGADDIN.VBP's Application Class
After you use the ConnectEvents method to connect an object to a Visual Basic menu or file control events, Visual Basic runs the appropriate method in that object when an event occurs. The method name and parameter count and type must match the definition that Visual Basic expects for the particular method.
Listing 24.5 shows the AfterClick method included in the REGADDIN.VBP sample. This method resides in the Application class, which also contains the ConnectAddIn method that creates the association between the menu item and the current object.
Listing 24.5 The AfterClick Method for REGADDIN.VBP's Application Class
The AfterClick method is a very simple application of an add-in. It merely shells another application—in this case, one that you use quite often when working with OLE: the Registration Info Editor.
Listings 24.6 and 24.7 are the definitions for the menu and file control events in Visual Basic. You can change the name of the arguments that these definitions use, but not their number, order, or type.
Listing 24.6 Add-In Event Definitions for the MenuLine Object
Listing 24.7 Add-In Event Definitions for the FileControl Object
Use the DisconnectEvents method to remove the association between a menu or FileControl object and an object in your application. The DisconnectEvents method suspends event trapping for the specified object.
The DisconnectAddIn method in the add-in's Application class undoes the actions taken in the ConnectAddIn method. Before Visual Basic unloads the add-in, you want your add-in to remove any items that it added to the Add-Ins menu and unload any forms that it displayed.
Listing 24.8 is the DisconnectAddIn method in the REGADDIN.VBP sample's Application class. The method closes the Registration Info Editor before the add-in unloads and removes the Add-Ins menu item if the user deselected the add-in in the Add-In Manager.
Listing 24.8 The DisconnectAddIn Method in the REGADDIN.VBP Sample's Application Class
Use the MenuItems collection's Add method to add an item to the Add-Ins menu. Visual Basic enables you to add items only to the Add-Ins menu. Other menus are off-limits for now.
The Add method returns a reference to the MenuLine object that you just added. You can use this reference in subsequent lines to associate the menu item with an object that detects the item's AfterClick event. The following line shows how to add a line displaying the text "Registration Info Editor" to the Add-Ins menu:
After adding the MenuLine object, you can connect it to an object that contains the AfterClick method to respond to click events on the MenuLine object. The following line connects the current object to the MenuLine object's AfterClick event:
You can also add submenus to the Add-Ins menu. A submenu is a menu that displays other menu items, which cascade downward as shown in figure 24.4.
You can add cascading submenus to the Add-Ins menu.
Use the MenuItems collection's AddMenu method to add a submenu to the Add-Ins menu. The AddMenu method returns a reference to the Menu object that you just added. You can use this reference in subsequent lines to add MenuLines or additional submenus. The following line shows how to add to the Add-Ins menu a submenu that displays the text "Submenu1":
Submenus can't detect click events the same way that MenuLine objects can. To add menu lines to a submenu, use the Add method on the Menu object's MenuItems collection. The following line shows how to add the line "MenuLine1" to the submenu Submenu1, which the previous line added:
Use the MenuItems collection's Remove method to delete an item from the Add-Ins menu. Deleting an item automatically removes all its subitems if the removed item is a submenu. The following line shows how to remove the Registration Info Editor line that you added in the previous section:
If you want to disable a menu line without removing it, set its Enabled property. The following line disables (grays) the Registration Info Editor line:
To disable a menu line without graying it or to associate a menu item with an AfterClick method in another object, use the DisconnectEvents method. The following line disassociates the Registration Info Editor line from the current object:
After using DisconnectEvents, you can associate the menu line with a new object by using the ConnectEvents method.
If Visual Basic has trouble locating an add-in or running the ConnectAddIn method, it displays the message shown in figure 24.5 when you try to load the add-in.
This error indicates a problem with the registration or ConnectAddIn event of an add-in.
If the add-in is correctly registered and its programmatic ID matches the one installed in VB.INI, the ConnectAddIn method probably has a problem.
You can debug an add-in by using two instances of Visual Basic. This is similar to the way that you debug any object library cross-process, but it involves a couple extra steps.
To debug an add-in, follow these steps:
You might want to set a breakpoint on the ConnectAddIn method within the add-in application to trace through initialization. After you set this breakpoint, you can rerun ConnectAddIn by unloading and reloading the add-in in the other instance of Visual Basic.
For more information on the following topics, see the indicated chapters:
© 1996, QUE Corporation, an imprint of Macmillan Publishing USA, a Simon and Schuster Company.