You are currently offline, waiting for your internet to reconnect

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

Support for Office 2003 has ended

Microsoft ended support for Office 2003 on April 8, 2014. This change has affected your software updates and security options. Learn what this means for you and how to stay protected.

This article was previously published under Q286460
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

  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.

    Picture of sample Picture.bmp. Steps				to re-create this image are given in this article.
  3. Save the image as Circle.bmp.
  4. 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.

    Picture of sample Mask.bmp. Steps to				re-create this image are given in this article.
  5. Save the image as Mask.bmp.

Steps to Create the COM AddIn

  1. Start Visual Basic and create a new AddIn project.
  2. 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.
  3. In the Project Explorer, right-click frmAddin and then click Remove frmAddin.
  4. In the Project Explorer, double-click the Connect designer. For Application select Microsoft Excel, and for Initial Load Behavior select Startup.
  5. 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					
  6. Save the project and build the Add-in in the folder in which the bitmaps are stored.
  7. Start Excel. A new control with a yellow circle appears on the standard CommandBar. Note that the area around the yellow circle is transparent.

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.
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 Boundaries
For more information, see the following Microsoft Web site:
addin ipicturedisp mask picture

Article ID: 286460 - Last Review: 01/29/2007 06:24:35 - 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
  • kbautomation kbhowto KB286460