Info

Figure 11.7 - The ShowFacelds toolbar provides a palette of images to use for custom buttons.

Use the ShowFacelDs toolbar to provide icon images for your custom toolbar buttons. Place the cursor over the desired command button to learn its FacelD property. You can copy the code for the cmdNewButton object shown a moment ago and paste it into the code that your own button creates. Just substitute the actual value for the FacelD icon that you need for the intCntr variable.

Adding a Command Bar to the Collection

Use the Add method to add a command bar to either the Inspector or the Explorer CommandBars collection. If you are using Visual Basic rather than VBScript, you can use named arguments when you call the Add method of the CommandBars collection object to add a command bar. The following example adds the Items command bar to the Explorer CommandBars collection:

Dim cbrNewToolbar As CommandBar

Set cbrNewToolbar = Application.ActiveExplorer.CommandBarsAdd _ (Name:="ltems", Position:=msoBarTop, Temporary:=False)

In VBScript, all variables are declared as Variants and named arguments are illegal. Here is a statement that adds an Inspector command bar:

Set objCommandBar= Item.Getlnspector.CommandBars.Add _ ("Command Bar Example", 1, False, True)

The Position property determines whether the toolbar will be docked in the Explorer or the Inspector window or displayed as a floating toolbar. If you supply the msoBarPopUp value for the Position argument, you will create a shortcut menu rather than a menu bar or toolbar. The following table shows the possible values for the Position property.

Constant Description msoBarLeft=0, msoBarTop=1, msoBarRight=2, Indicates the left, top, right, and bottom coordinates of the new msoBarBottom=3 command bar msoBarFloating=4 Indicates that the new command bar won't be docked msoBarPopup=5 Indicates that the new command bar will be a shortcut menu

The Temporary property indicates whether the command bar will be deleted when the Outlook application or the Inspector window closes. Notice that the VBScript Command Bar Example toolbar is temporary; it appears only for a specific custom form. Generally, you don't want to create persistent Inspector toolbars.

Once you have added the command bar to the CommandBars collection, you can set additional properties that determine the command bar's appearance and behavior. For example, the Type property determines whether the CommandBar object behaves as a menu bar, toolbar, or shortcut menu.

Retrieving an Existing Command Bar

To retrieve an existing built-in or custom command bar, use the Items property with either an index value or the name of the command bar. Because the Items property is the default property of the CommandBars collection object, you don't actually have to use the Items property to retrieve a command bar from a collection. The following two statements are equivalent:

Set cbrltems = Application.ActiveExplorer.CommandBars.ltems("ltems") Set cbrltems = Application.ActiveExplorer.CommandBars("ltems")

Deleting a Command Bar

Use the Delete method to delete a custom command bar from the CommandBars collection. You cannot delete a built-in command bar with the Delete method. The Delete method is actually a CommandBar method, not a CommandBars method. The following function deletes a command bar and returns True if the command bar was successfully deleted:

Function CBDeleteCommandBar(strCBarName As String) As Boolean On Error Resume Next

Application ActiveExplorer.CommandBars(strCBarName). Delete If Err = 0 Then

CBDeleteCommandBar = True End If End Function

Using the Onllpdate Event

The Command Bars collection supports an OnUpdate event that fires whenever a command bar is changed. For additional information regarding Visual Basic events, the WithEvents keyword, and event procedures, see Chapter 9. The event is triggered by any change to a command, or by the state of a bar or command bar control. These changes can result from pressing a button, by changing text, or by selecting a cell. Because a large number of OnUpdate events can occur during normal usage, you should be very cautious when working with this event. I strongly recommend that you use this event primarily for checking that a custom command bar has been added or removed by an Outlook COM Add-in. To see how many times this event can fire, add the following code to VBAProject.otm. A statement appears in the Debug window every time the OnUpdate event fires.

'Place in Declarations of ThisOutlookSession Dim WithEvents colCB As CommandBars

Private Sub Application_Startup()

Set colCB = Application ActiveExplorer.CommandBars End Sub

Private Sub colCB_OnUpdate()

Debug.Print "CommandBars OnUpdate" End Sub

The CommandBar Object

The CommandBar object represents a command bar—either built-in or custom—for either the Outlook Explorer or Inspector window. You can modify a built-in command bar through the Customize dialog box or through program code. If you are building a custom Outlook application or creating a COM Add-in, you should consider providing a custom command bar or modifying the menus on the Outlook menu bar for your COM Add-in. Once you have created a command bar object programmatically, you should add controls to the command bar and program them to respond to events such as Click or Change.

Properties of the CommandBar Object

The following table lists some important properties of the CommandBar object. For additional information on CommandBar object properties, see Microsoft Outlook Visual Basic Reference Help.

Property

Description

AdaptiveMenus Determines whether an individual command bar displays adaptive menus. This property can be True or False

Enabled Determines whether the command bar can be modified with the Customize dialog box. If the Enabled property is

False, you cannot set the Visible property to True .

Position Returns or sets the position of the command bar.

Type Determines the type of command bar—menu bar, toolbar, or shortcut menu.

Visible Determines whether the command bar is visible in the Explorer or Inspector window. This property can be True or

False .

Methods of the Command Bar Object

The methods listed in the following table operate on the CommandBar object. For additional information on methods for the CommandBar object, see Microsoft Office Visual Basic Reference Help.

Method Description

Delete Deletes a custom command bar.

FindControl Finds a control on a command bar that meets specified criteria. You can search by control type, ID, or tag property based on whether a control is visible or on a submenu.

Reset Resets a built-in command bar to its default configuration. If you have customized a built-in command bar, those customizations are removed when you call thismethod.

ShowPopup Shows the command bar as a pop-up menu at specified coordinates or at the current cursor location.

The ShowPopup method will work only for a command bar that has been added as a shortcut menu to the CommandBars collection. You cannot cause a built-in or custom toolbar to appear suddenly as a pop-up menu. The Outlook Inspector has a limited capacity to display shortcut menus because a MouseUp event is not supported using the current control container for Outlook custom forms. If you are utilizing UserForms in your VBA or COM Add-in project, use the MouseUp event of intrinsic and third-party ActiveX controls to build event procedures that display Outlook shortcut menus. The following procedure displays a pop-up menu at the current cursor location in the Explorer window:

Sub ExplorerPopUp()

Dim CBCopyandPasteMenu As Office.CommandBar

Set CBCopyandPasteMenu = Application.ActiveExplorer.CommandBars.Add

(Name:="Custom", Position:=msoBarPopup, Temporary:=True) Set Copy = CBCopyandPasteMenu.Controls.Add With Copy

.Faceld = Application. ActiveExplorer.CommandBars _

("Menu Bar").Controls("Edit").Controls("Copy").ID .Caption = "Copy the selection" End With

Set Paste = CBCopyandPasteMenu.Controls.Add

With Paste

.Faceld = Application. ActiveExplorer.CommandBars _

("Menu Bar").Controls("Edit").Controls("Paste").ID .Caption = "Paste from the Clipboard" End With

CBCopyandPasteMenu.ShowPopup End Sub

The next procedure uses the MouseUp event to display the Data Helper shortcut menu when the right mouse click occurs over the IstSync control on a UserForm:

Private Sub lstSync_MouseUp _ (ByVal Button As Integer, ByVal Shift As Integer, _ ByVal X As Single, ByVal Y As Single) On Error Resume Next Dim cbrShortcut As CommandBar Select Case Button

Case vbKeyLButton 'Left Case vbKeyRButton 'Right Set cbrShortcut = _

Application.ActiveExplorer.CommandBars("Data Helper") cbrShortcut. Visible = True cbrShortcut.ShowPopup X, Y End Select End Sub

Protecting a Command Bar from User Modification

How do you protect a custom command bar you've developed from user modification? A user can modify your command bar either through the user interface or through program code. To prevent changes to your custom command bar, you can set the Enabled property to False to make the command bar invisible in the list of Outlook command bars for either the Explorer or Inspector window. Once the Enabled property is set to False, users cannot modify your custom command bar because they will not be able to see the command bar name in the Toolbars list. You must reset the Enabled property to True before you can use the Visible property to display the command bar. However, the Enabled property is not the most secure means of protecting your custom command bar. The following procedure can prevent a user from using the Customize dialog box to alter your custom command bar. The code disables the Customize command on the Tools menu and the Toolbars list on the View menu.

Sub AllowExplorerCBCustomization(blnAllowEnabled As Boolean) 'This procedure allows or prevents access to the 'command bar's Customize dialog box according to the 'value of the blnAllowEnabled argument. Dim colCB As CommandBars

Set colCB = Application.ActiveExplorer.CommandBars colCB("Tools").Controls("Customize..."). Enabled = blnAllowEnabled colCB("Toolbar List").Enabled = blnAllowEnabled End Sub

0 0

Post a comment