More programming for Microsoft Office command bars (1)

Source: Internet
Author: User
Tags interface object model transparent image valid microsoft outlook
Programming more programming for Microsoft Office command bars
Frank C. Rice.
Microsoft Corporation
Paul Cornell
Microsoft Corporation
May 2, 2002

In last month's column, I introduced you to the command bar, which is a user interface component used in Microsoft®office that enables users to perform actions in an Office application. Based on your feedback to last month's column, I'll introduce you to some additional information and code for resolving command bar-specific issues, including the following:

Microsoft Outlook® How to programmatically process command bars.
How to add a custom image to a command bar button.
How to add a combo box to the command bar.
How to disable and hide command bars and command bar controls.
How to position the command bar.
How to dynamically add and remove command bars.
How to list the common properties of command bars and command bar controls in a given Office application.
command bar and Outlook object model
In last month's column, I forgot to tell you that the Microsoft Outlook object Model accesses command bars and command bar controls in a slightly different way than other Microsoft Office applications.

In applications other than Outlook, you can access the command bar using code such as the following:

Public Sub Listcommandbarnames ()

' Purpose: Lists all command bar names for the current application.
' Note: This code is not valid for Outlook!

Dim objCommandBar as Office.CommandBar

For each objcommandbar in Application.CommandBars

Debug.Print Objcommandbar.name

Next objCommandBar

End Sub

However, trying to run this code in Outlook will result in a run-time error. Instead, you must use the CommandBars property of the Explorer or Inspector object, as follows:

Public Sub Listoutlookexplorercommandbarnames ()

' Purpose: Lists all of the command bar names for the current resource manager.
' Note: This code is only valid for Outlook!

Dim objCommandBar as Office.CommandBar

For each objcommandbar in Application.ActiveExplorer.CommandBars

Debug.Print Objcommandbar.name

Next objCommandBar

End Sub

In the preceding code example, replace the code activeexplorer with all the command bar names that ActiveInspector can print the activity inspector. For those who are unfamiliar with the Outlook object model, the browser represents the Outlook user interface. Inspector represents a window that contains a specific Outlook item, such as an e-mail message or contact, and any tab pages in an Outlook item, such as the Details tab in a task item.

Add a custom image to a command bar button
Although you can use the CommandBarButton object's FaceId property to set the image of a command bar button to a built-in image that Office provides, you can use the Picture property of the CommandBarButton object to provide the image you create, or you can use Com Mandbarbutton the Mask property of the object to create a custom transparent image.

Although there are many image editors available on the web's shareware and free software sites, as well as tools such as Microsoft Visual c++®, it is sufficient to create these images using Microsoft Paint. To create these images using Paint:

On the Start menu, point to Programs, point to Accessories, and then click Paint.
On the Image menu, click Properties.
In the Width box, type 16. In the Height box, type 16. Make sure the pixel and color options are selected, and then click OK.
On the View menu, point to Zoom, and then click Customize.
Click the 800% option, and then click OK.
On the View menu, point to Zoom, and then click Show Grid.
On the View menu, make sure that the toolbox and color box commands are selected.
Use the Toolbox and the color box controls to create an image.
After you finish creating the image, on the File menu, click Save.
Save the icon as "256-color bitmap."
Here is an example of the image I created:



Figure 1: Custom opaque bitmap

To create a transparent image, you must create the appropriate image mask. To do this, save the image you just created, but change the file name. For each pixel that needs to be transparent, fill the pixel with white. For each pixel that needs to be opaque, fill the pixel with black. Then save the image again. The following is an example of an image mask I created:



Figure 2: Custom Bitmap Masks

Here are some sample code that shows how to add a transparent picture to a command bar button:

Public Sub Newpictureonnewcommandbar ()

' Purpose: Add a picture to the command bar button.

Dim objCommandBar as Office.CommandBar
Dim objCommandBarButton as Office.CommandBarButton
Dim Objpicture as stdole. IPictureDisp

' If you don't need a transparent image, comment off the next line of code.
Dim Objmask as stdole. IPictureDisp

Const Picture_path as String = "C:\My pictures\ok.bmp"

' If you don't need a transparent image, comment off the next line of code.
Const Picture_mask as String = "C:\My pictures\okmask.bmp"

Const command_bar_name as String = "Test command bar"

' Replace the next line with the following:
' For each objcommandbar in Application.ActiveExplorer.CommandBars <-for Outlook
' For each objcommandbar in Application.VBE.CommandBars <-for Visual Basic Editor
For each objcommandbar in Application.CommandBars

If objcommandbar.name = Command_bar_name Then

Objcommandbar.delete

End If

Next objCommandBar

Set objCommandBar = APPLICATION.COMMANDBARS.ADD (command_bar_name)
Set objCommandBarButton = _
OBJCOMMANDBAR.CONTROLS.ADD (msoControlButton)
Set objpicture = LoadPicture (Picture_path)

' If you don't need a transparent image, comment off the next line of code.
Set objmask = LoadPicture (picture_mask)

Objcommandbarbutton.picture = Objpicture

' If you don't need a transparent image, comment off the next line of code.
Objcommandbarbutton.mask = Objmask

End Sub

As shown in the previous code, create an object for an opaque image in your code, create an object for the image mask, and each object must be stdole. IPictureDisp type. Next, the two objects are initialized separately by calling the object's LoadPicture method. Finally, set the Picture property of the command bar button object to an opaque image object, setting the Mask property of the command bar button object to the image mask.

The following is the final appearance of opaque and transparent images:



Figure 3: Opaque and transparent images on command bar buttons

By the way, do not confuse the FaceId property of the CommandBarButton object with the Id attribute of the CommandBarButton object. The Id property determines the built-in operation of the command bar control. The default values for all custom command bar control Id properties are 1. Setting the ID property of a custom command bar control to a number other than 1 sets the operation of the custom command bar control to a built-in action, provided that there is a built-in action for this ID in the application. For ease of reference, the following code lists all the IDs for all command bar controls in your application:

Public Sub Listcommandbarcontrolids ()

' Purpose: Lists the IDs of all command bar controls for the current application.

Dim objCommandBar as Office.CommandBar
Dim Objcommandbarcontrol as Office.commandbarcontrol

' Replace the next line with the following:
' For each objcommandbar in Application.ActiveExplorer.CommandBars <-for Outlook
' For each objcommandbar in Application.VBE.CommandBars <-for Visual Basic Editor
For each objcommandbar in Application.CommandBars

For each objcommandbarcontrol in objCommandBar.Controls

Debug.Print Objcommandbarcontrol.caption & "" & _
Objcommandbarcontrol.id

Next Objcommandbarcontrol

Next objCommandBar

End Sub

Add a combo box to the command bar
To add a combo box to the command bar, use the Add method of the CommandBarControls collection and pass the msoControlComboBox enumeration constant to the Type parameter. Then use the CommandBarComboBox object's AddItem method to add options to the combo box.

The following function adds a combo box to an existing command bar:

Public Function Addcomboboxtocommandbar (ByVal strcommandbarname as String, _
ByVal Strcomboboxcaption as String, _
ByRef strchoices () as String) as Boolean

' Purpose: Add a combo box to the command bar.
Accept
' Strcommandbarname: The name of the command bar that adds the combo box.
' Strchoices (): Array of combo box options.
' Return: True if the combo box has been successfully added to the command bar.

Dim Objcommandbarcontrol as Office.commandbarcontrol
Dim Objcommandbarcombobox as Office.commandbarcombobox
Dim VarChoice as Variant

On Error GoTo Addcomboboxtocommandbar_err

' Delete all instances of this combo box that you added previously.
' Replace the next line of code with the following:
' For each Objcommandbarcontrol in _
' Application.ActiveExplorer.CommandBars.Item (strcommandbarname). Controls _
<-for Outlook
' For each Objcommandbarcontrol in _
' Application.VBE.CommandBars.Item (strcommandbarname). Controls _
<-for Visual Basic Editor
For each Objcommandbarcontrol in Application.CommandBars.Item (Strcommandbarname). Controls

If objcommandbarcontrol.caption = strcomboboxcaption Then

Objcommandbarcontrol.delete

End If

Next Objcommandbarcontrol

' Create a combo box.
' Replace the next line of code with the following:
' Set Objcommandbarcombobox = _
' Application.CommandBars.Item (strcommandbarname). Controls.Add (msoControlComboBox) _
<-for Outlook
' Set Objcommandbarcombobox = _
' Application.CommandBars.Item (strcommandbarname). Controls.Add (msoControlComboBox) _
<-for Visual Basic Editor
Set Objcommandbarcombobox = _
Application.CommandBars.Item (Strcommandbarname). Controls.Add (msoControlComboBox)

Objcommandbarcombobox.caption = Strcomboboxcaption

For each varchoice in strchoices

Objcommandbarcombobox.additem VarChoice

Next VarChoice

Addcomboboxtocommandbar_end:

Addcomboboxtocommandbar = True
Exit Function

Addcomboboxtocommandbar_err:

Addcomboboxtocommandbar = False

End Function



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.