Microsoft Knowledge Base Email Alertz

KBAlertz.com: (286460) - 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...

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 286460 - Last Review: January 29, 2007 - Revision: 4.5

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

This article was previously published under Q286460

On This Page

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 "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.

    Collapse this imageExpand this image
    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.

    Collapse this imageExpand this image
    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 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 & "\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 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 "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.

REFERENCES

For additional information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
238228  (http://kbalertz.com/Feedback.aspx?kbNumber=238228/ ) How To Build an Office 2000 COM Add-In in Visual Basic
230689  (http://kbalertz.com/Feedback.aspx?kbNumber=230689/ ) SAMPLE: Comaddin.exe Office 2000 COM Add-In Written in Visual C++
150034  (http://kbalertz.com/Feedback.aspx?kbNumber=150034/ ) PRB: LPPICTUREDISP Cannot Be Passed Across Process Boundaries
For more information, see the following Microsoft Web site:
http://support.microsoft.com/ofd (http://support.microsoft.com/ofd)

APPLIES TO
  • 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 Excel 2002 Standard Edition
  • Microsoft Word 2002
  • Microsoft FrontPage 2002 Standard Edition
  • Microsoft Outlook 2002 Standard Edition
  • Microsoft Office PowerPoint 2003
  • Microsoft PowerPoint 2002 Standard Edition
Keywords: 
kbautomation kbhowto KB286460
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please