How To Set the Mask and Picture Properties for Office XP CommandBars
This article was previously published under Q286460
This article has been archived. It is offered "as is" and will no longer be updated.
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.
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 "References" 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
- Start Microsoft Paint. On the Image menu, click Attributes. Change the image size to 32x32 and click OK.
- Draw a yellow face on the painting surface. Fill the area surrounding the circle with blue.
- Save the image as Circle.bmp.
- 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.
- 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 "Microsoft Office" 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 ExplicitDim oExcel As ObjectDim WithEvents oButton As Office.CommandBarButtonPrivate 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 & "\circle.bmp") Set oMask = LoadPicture(App.Path & "\mask.bmp") ' Save an instance of our application. Set oExcel = Application ' Create a new button on the standard CommandBar. Set oButton = oExcel.CommandBars("Standard").Controls.Add(msoControlButton) With oButton ' Set a tag for the button. .Tag = "My Button" ' Set the event to fire when the button is pressed. .OnAction = "!<" & AddInInst.ProgId & ">" ' 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 WithEnd SubPrivate 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 SubPrivate Sub oButton_Click(ByVal Ctrl As Office.CommandBarButton, _ CancelDefault As Boolean) ' Our button was pressed. MsgBox "The button was pressed!"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.
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)':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.
Method 'Picture' of object '_CommandBarButton' failed
Method 'Picture' of object '_CommandBarButton' failed
For additional information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
238228 How To Build an Office 2000 COM Add-In in Visual Basic
230689 SAMPLE: Comaddin.exe Office 2000 COM Add-In Written in Visual C++
150034 PRB: LPPICTUREDISP Cannot Be Passed Across Process BoundariesFor more information, see the following Microsoft Web site:
addin ipicturedisp mask picture
Article ID: 286460 - Last Review: 12/06/2015 00:09:06 - Revision: 4.6
Microsoft Visual Basic Enterprise Edition for Windows 6.0, 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, 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
- kbnosurvey kbarchive kbautomation kbhowto KB286460