After you set up the basic COM add-in project, you'll need to determine how the add-in will function in an Office 2000 application. Most add-ins will be exposed, or made available to the end user, through a custom menu or toolbar button. Generally, the add-in adds command bar customizations. The end user clicks on the customized menu or button, which executes some code in the add-in or displays a custom dialog box.
Other add-ins don't have a visible user interface like command bar customizations. Some add-ins will be loaded when the application is started and work in the background, handling events such as the opening, closing, or saving of documents. The examples in this section describe the code that allows your add-in to add a menu item that displays a custom dialog box . You can also set up your add-in to trap events from an Office application such as those described in chapters 5 and 10.
When a COM add-in is loaded in an Office application, the first code that's executed is the code in the OnConnection event procedure. Each Office application initializes its menus and toolbars when the application's initialization sequence executes, and you should place code that customizes the application's command bars in the OnConnection procedure.
The following steps explain how to structure your code so that all command bar customization code is in one place, making the add-in project simpler, more readable, and easier to expand.
When a COM add-in is loaded and the OnConnection procedure executes, your code should first ensure any command bar customizations previously added by your add-in exist. If they don't, the procedure given at the end of the following steps adds the specific customizations to the command bar set.
The Microsoft Office 9.0 Object Library defines the objects, methods, properties, and events that allow you to manipulate menus or toolbars, or to add custom menus and toolbars in an Office application. Chapter 11," Creating Menus and Toolbars," provides a complete description of how to use the CommandBars collection object to customize menus and toolbars.
Public HostApp As Object Dim HostCmdBars As Office.CommandBars Dim CBEvents As New CmdBarEvents Const m_sBtn1Caption As String = "Button1" Const m_sBtn1Tag As String = "Button1" |
The first declaration declares the variable HostApp as the generic type Object; HostApp represents the Office application that the add-in is currently loaded into. (The HostApp variable will be set in the OnConnection procedure in a later step.)
The variable HostCmdBars is declared as the type CommandBars, which is defined in the Microsoft Office 9.0 Object Library. All Office applications access the CommandBars collection object. Here, the variable is used to store the CommandBars collection object of the Office application that the add-in is currently loaded into. The HostCmdBars variable will be set in the InitCmdBarCustomizations procedure, which is listed in the next step.
The CBEvents object variable references the class CmdBarEvents. For information on how to add this class, see the "Setting Up the Button Click Event Procedure" section following the current set of steps. The CmdBar-Events class contains command bar event procedures for each custom command bar button added.
The last two declarations are constants that are used to set the properties of the custom menu command that will be added. The strings are declared as constants at the beginning of the code module, so if they change, you need to change them only in one place in your code.
Sub InitCmdBarCustomizations() GetCommandBars If HostCmdBars.FindControls(Tag:=m_sBtn1Tag) _ Is Nothing Then AddMenuCtrls Else Set CBEvents.CtlBtn1 = HostCmdBars _ .FindControls(Tag:=m_sBtn1Tag).Item(1) End If End Sub |
The first line of the InitCmdBarCustomizations procedure calls the Sub procedure GetCommandBars, which will be added in the following steps. The GetCommandBars procedure sets the variable HostCmdBars. The If…Then…Else block uses the FindControls method on the CommandBars collection object to determine if any control in the command bar set has the custom string tag equal to the constant m_sBtn1Tag.
If the FindControls method doesn't find any controls that match the search criteria, it returns a value of Nothing. This causes the If…Then expression to evaluate to True and the function AddMenuCtrls is called. If the FindControls method returns one or more existing controls that match the search criteria, the Click event is set up for the existing control(s).
Sub AddMenuCtrls() Dim ctlBtn As CommandBarButton Set ctlBtn = HostCmdBars("Tools").Controls.Add With ctlBtn .Caption = m_sBtn1Caption .Tag = m_sBtn1Tag Set CBEvents.CtlBtn1 = ctlBtn End With End Sub |
Sub GetCommandBars() Select Case HostApp.Name Case "Outlook" Set HostCmdBars = HostApp _ .ActiveExplorer.CommandBars Case Else Set HostCmdBars = HostApp.CommandBars End Select End Sub |
This example assigns the HostCmdBars variable to the CommandBars collection object in an Office application. This lets you retrieve the collection once and store it globally for use by all the code in your add-in that manipulates the command bars. In Outlook, the CommandBars collection object can be retrieved through the active Explorer window (or any Explorer object). In all other Office applications, the CommandBars collection object is retrieved through the Application object.
Set HostApp = Application InitCmdBarCustomizations |
When Office calls the OnConnection procedure, an instance of the Application object (of the application into which the add-in is being loaded) is passed in. The Application object is specified in the first argument of the OnConnection procedure. The name of the argument is Application and it is declared as the generic type Object.
The Set statement sets the public variable HostApp, which was declared in the CmdBarInit code module, to the Application object passed into the OnConnection procedure. The second line is the call to the InitCmdBarCustomizations procedure, which was added in the CmdBarInit code module.
The following steps describe how to set up the Click event procedure. Once set up, the procedure will be called every time a specific command bar button is clicked by the user, allowing code you add in the Click event procedure to be executed.
Public WithEvents CtlBtn1 As Office.CommandBarButton |
This inserts the Click event procedure for the command bar button.
MsgBox Ctrl.Caption |
If you scroll through the Insert, Format, or Tools menus on any Office application, you'll notice that a dialog box appears whenever you click a menu command that contains an ellipsis (...) in its caption string. To follow this standard Windows user-interface design guideline, in the following steps you add the code to the Click event procedure associated with the custom menu item that displays that dialog box. (That is, you add an ellipsis to the custom menu item added by the COM add-in.)
Property | Value |
---|---|
BorderStyle | 3 – Fixed Dialog |
MinButton | False |
MaxButton | False |
ShowInTaskbar | False |
WhatsThisButton | False |
Dialog.Show Modal:=vbModal |
The Show method takes two arguments. The Modal argument indicates whether a form should be displayed as modal or modeless. Dialog boxes are generally displayed as modal, which means that the user can't interact with the Office application until the dialog box has been dismissed.
NOTE
Although the Modal argument in Visual Basic is optional and the default form display for the Show method is modal, you should explicitly set the Modal argument to vbModal when displaying a form from a COM add-in. If you don't explicitly set the argument, the form will be displayed as modeless and it will not appear in front of the Office application window. In this case, you need to set the parent window of the modeless to the Office application window.
Const m_sBtn1Caption As String = "Button1..." |
If you chose to run the project in Visual Basic rather than compile the project and then let the Office application load the compiled project, the resulting dialog box will be displayed in front of the Visual Basic window. This is similar to the custom dialog box behavior that occurs when you debug your Visual Basic code. If you compiled the add-in into a .dll without running the Visual Basic project, custom dialog boxes and message boxes will appear in front of the Office application.
When a COM add-in is loaded and the OnConnection procedure is executed, code that you added in the steps for "Adding a Menu Command When a COM Add-In Loads" determines if any command bar customizations added by your add-in already exist. If they don't, a procedure that adds the specific customizations to the command bar set is executed.
When an add-in is unloaded and the OnDisconnection procedure executes, your code conversely should determine whether command bar customizations should be removed. Notice that the following steps are similar to the steps for adding menu commands when a COM add-in loads.
If RemoveMode = ext_dm_UserClosed Then DeleteMenuCtrls End If |
The first argument passed to the OnDisconnection event procedure is the RemoveMode value. The two values passed in are ext_dm_Host-Shutdown and ext_dm_UserClosed. The value of ext_dm_UserClosed is passed into the RemoveMode argument when you unload an add-in through the COM Add-Ins dialog box. When the add-in is cleared in the COM Add-Ins dialog box, the add-in is no longer available. Although the add-in is still registered, you should remove command bar customizations from the command bar set when the user unloads the add-in.
The value of ext_dm_HostShutdown is assigned to the RemoveMode argument when the Office application unloads the add-in while the application is exiting. In this case, you should retain command bar customizations. This way, the customizations will already be present the next time the Office application is started, without executing the code to add them. (In most cases, the application will unload add-ins when you exit it.)
Sub DeleteMenuCtrls() Dim ctlItem As CommandBarControl Dim ctlColl As CommandBarControls Set ctlColl = HostCmdBars.FindControls(Tag:=m_sBtn1Tag) If Not ctlColl Is Nothing Then For Each ctlItem In ctlColl ctlItem.Delete Next ctlItem End If End Sub |
Add-ins that just add customizations to a command bar and execute code only when a custom menu item or toolbar is clicked don't need to be loaded when the Office application starts. Instead, you can have the add-in load on demand—that is, when the user clicks on a customization, the add-in loads into memory immediately following the click event.
To add your custom commands after your add-in is installed, you need to set the initial load behavior of the COM add-in so that it loads the next time the Office application starts. Office then sets the load behavior registered for the add-in so the custom command demand-loads the add-in the next time the Office application is started.
Because your add-in is loaded only on demand, it does not get loaded when the Office application is started, and thus does not affect the time it takes to start an Office application. Add-ins that are loaded when the Office application is started increase the time it takes to start the Office application.
Sub AddMenuCtrls() Dim ctlBtn As CommandBarButton Set ctlBtn = HostCmdBars("Tools").Controls.Add With ctlBtn .Caption = m_sBtn1Caption Select Case HostApp.Name Case "Microsoft Word" .OnAction = "!<BasicCOMAddIn.ConnectWord>" End Select .Tag = m_sBtn1Tag Set CBEvents.CtlBtn1 = ctlBtn End With End Sub |
When an add-in is set to load on demand, Office needs a way to associate a command bar control with a COM add-in. Office determines the association by evaluating the OnAction string assigned to the custom command bar control. In the Select Case statement above, the OnAction string is assigned to the programmatic identifier (or ProgID) of the COM add-in when it is loaded into Word. In the section "Adding Another Office Application to the Add-In" after the following steps, the ProgID will be BasicCOMAddIn.ConnectExcel and assigned to the OnAction property when the add-in is loaded into Excel. (The ProgID allows you to uniquely identify your COM add-in and distinguish it from other COM add-ins.)
See steps 2 and 3 in the section "Test the COM Add-In" to learn how to find the COM Add-Ins command. In the COM Add-Ins dialog box, with Microsoft Office Basic COM Add-In for Word selected in the list, the load status will be "Load on Demand, (not currently loaded)."
With Microsoft Office Basic COM Add-In for Word selected in the list, the current load status will be "Load on Demand, (currently loaded)."
The user doesn't have to explicitly load the add-in through the COM Add-Ins dialog box. Once the add-in is set up on the user's machine, the next time the user starts Word, the COM add-in loads and Button1 is added. Thereafter, the next time the user starts the application, the add-in isn't loaded at startup, but only when the user clicks the custom command on the Tools menu.
NOTE
For information on using the Package and Deployment wizard to deploy your add-in onto a user's machine, see the section "Packaging and Deploying Your COM Add-In" near the end of this chapter.
Add-in Designer window | Setting |
---|---|
Addin Display Name edit box | Add the text "Microsoft Office Basic COM Add-In for Excel." |
Application drop-down | Microsoft Excel |
Application Version drop-down | Microsoft Excel 9.0 |
Initial Load Behavior drop-down | None |
Set HostApp = Application InitCmdBarCustomizations |
If RemoveMode = ext_dm_UserClosed Then DeleteMenuCtrls End If |
Select Case HostApp.Name Case "Microsoft Word" .OnAction = "!<BasicCOMAddIn.ConnectWord>" Case "Microsoft Excel" .OnAction = "!<BasicCOMAddIn.ConnectExcel>" End Select |
In the Select Case statement above, the OnAction string is assigned to the programmatic identifier (or ProgID) of the COM add-in when it is loaded into Excel. When the add-in is loaded into Excel, the ProgID is BasicCOMAddIn.ConnectExcel.