Microsoft KB Archive/286460

= How To Set the Mask and Picture Properties for Office XP CommandBars =

Article ID: 286460

Article Last Modified on 1/29/2007

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Office Access 2003
 * Microsoft Access 2002 Standard Edition
 * Microsoft Office Excel 2003
 * Microsoft Excel 2002 Standard Edition
 * Microsoft Office Word 2003
 * Microsoft Word 2002 Standard Edition
 * Microsoft Office FrontPage 2003
 * Microsoft FrontPage 2002 Standard Edition
 * Microsoft Office Outlook 2003
 * Microsoft Outlook 2002 Standard Edition
 * Microsoft Office PowerPoint 2003
 * Microsoft PowerPoint 2002 Standard Edition

-



This article was previously published under Q286460



SUMMARY
Microsoft Office XP introduces two new properties to the CommandBarButton object: the Mask and Picture properties. You can use these properties to place pictures on custom generated command bar controls.

Note These properties are also available in Microsoft Office 2003.



MORE INFORMATION
The Mask and Picture properties are defined as type IPictureDisp, which is a member of the Stdole library. IPictureDisp uses methods that cannot be marshalled across process boundaries. Therefore, the Mask and Picture properties can only be called in-process (VBA macros, Automation Add-ins, and ActiveX DLLs run in-process). See the &quot;References&quot; section for more information regarding this behavior.

The sample below shows how to create an Automation Add-in that adds a CommandBar button with a masked picture.

Steps to Create Images for the Picture and the Mask

 * 1) Start Microsoft Paint. On the Image menu, click Attributes. Change the image size to 32x32 and click OK.
 * 2) Draw a yellow face on the painting surface. Fill the area surrounding the circle with blue.


 * 1) Save the image as Circle.bmp.
 * 2) To create the mask, fill the circle with black and fill the area outside the circle with white. When the picture with the mask is added to the CommandBar control, the black areas of the mask are visible, while the white areas are transparent.


 * 1) Save the image as Mask.bmp.

Steps to Create the COM AddIn
 Start Visual Basic and create a new AddIn project. On the Project menu, click References. If a &quot;Microsoft Office&quot; type library earlier than version XP is selected, clear that type library and select the type library for Microsoft Office XP. Click OK. In the Project Explorer, right-click frmAddin and then click Remove frmAddin. In the Project Explorer, double-click the Connect designer. For Application select Microsoft Excel, and for Initial Load Behavior select Startup.  On the View menu, click Code and replace the code for the Add-in with the following: Option Explicit

Dim oExcel As Object Dim WithEvents oButton As Office.CommandBarButton

Private Sub AddinInstance_OnConnection(ByVal Application As Object, _ ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _ ByVal AddInInst As Object, custom As Variant)

Dim oPic As stdole.IPictureDisp Dim oMask As stdole.IPictureDisp

' Load the picture and mask. Set oPic = LoadPicture(App.Path & &quot;\circle.bmp&quot;) Set oMask = LoadPicture(App.Path & &quot;\mask.bmp&quot;)

' Save an instance of our application. Set oExcel = Application ' Create a new button on the standard CommandBar. Set oButton = oExcel.CommandBars(&quot;Standard&quot;).Controls.Add(msoControlButton) With oButton ' Set a tag for the button. .Tag = &quot;My Button&quot; ' Set the event to fire when the button is pressed. .OnAction = &quot;!<&quot; & AddInInst.ProgId & &quot;>&quot; ' Set the picture property -- if you are using the Mask property, this ' property must be set before you set the Mask property. .Picture = oPic ' Set the Mask property. .Mask = oMask ' Show the button. .Visible = True End With End Sub

Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode As _  AddInDesignerObjects.ext_DisconnectMode, custom As Variant)

' Delete the button. oButton.Delete ' Release references. Set oButton = Nothing Set oExcel = Nothing End Sub

Private Sub oButton_Click(ByVal Ctrl As Office.CommandBarButton, _ CancelDefault As Boolean) ' Our button was pressed. MsgBox &quot;The button was pressed!&quot; End Sub  Save the project and build the Add-in in the folder in which the bitmaps are stored. Start Excel. A new control with a yellow circle appears on the standard CommandBar. Note that the area around the yellow circle is transparent.</li></ol>

Additional Notes

As previously stated, the IPictureDisp interface cannot be marshalled across process boundaries. Attempts to set the Picture and Mask properties out-of-process result in the following error:

Run-time error '-2147418113(8000ffff)':

Method 'Picture' of object '_CommandBarButton' failed

Therefore, you cannot set these properties for a CommandBarButton from any out-of-process Automation client or from an in-process component that is running in debug mode in the Visual Basic IDE. If you run the sample Add-in described in this article from the Visual Basic IDE, you will receive this error. To avoid the error while debugging, you can comment the lines that set the Picture and Mask properties or use conditional compilation, such as an #If...Then...#Else directive.

<div class="references_section">