Results 1 to 1 of 1

Thread: [FAQ's: OD] How do I add a custom image to the menu item or toolbar button?

Threaded View

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    [FAQ's: OD] How do I add a custom image to the menu item or toolbar button?

    To add a custom image to your toolbar button or menu item you need to use the .Picture property of the commandbarbutton object of an Office 2000 or newer application. If your running Office 2002 or newer then you will also have the .Mask property for creating an image with a "transparent" background.


    Here is how the image and mask look zoomed in 800%




    One important note: The .Picture and.Mask properties of a toolbar button or menu item are only valid for processes that dont attempt cross process marshalling such as the VBA ThisOutlookSession or other code in the VBA IDE. For a work around see my upcoming FAQ - "Why wont my custom image show on a menu item or toolbar button?"


    Word 2003 VBA Code Example:


    VB Code:
    1. Option Explicit
    2.  
    3. '<CREATE AN EVENT HANDLER>
    4. Public WithEvents oCBBCustom As Office.CommandBarButton
    5.  
    6. '<BUTTON CLICK EVENT PROCEDURE>
    7. Private Sub oCBBCustom_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    8.     MsgBox "Meow!!!"
    9. End Sub
    10.  
    11. Private Sub Document_Open()
    12.  
    13.     Dim oCB As Office.CommandBar
    14.     Dim oCBBTools As Office.CommandBarPopup
    15.  
    16.     Dim oPic As stdole.IPictureDisp
    17.     Dim oMask As stdole.IPictureDisp
    18.  
    19.     '<LOAD THE PICTURE AND MASK OBJECTS>
    20.     Set oPic = LoadPicture("C:\Cat.bmp")
    21.     Set oMask = LoadPicture("C:\CatMask.bmp")
    22.  
    23.     Set oCB = Application.CommandBars("Menu Bar")
    24.     Set oCBBTools = oCB.Controls("&Tools")
    25.     [color=darkgreen]'<ADD A NEW BUTTON>[/color]
    26.     Set oCBBCustom = oCBBTools.Controls.Add(msoControlButton, 1, , , True) '<PLACE OUR CUSTOM ITEM AT THE BOTTOM>
    27.     With oCBBCustom                                                        '<POSITION (0 BASED)>
    28.         .Caption = "Meow!"
    29.         .BeginGroup = True '<ADD A MENU SEPARATOR>
    30.         .Enabled = True
    31.         .Picture =  oPic '<OFFICE 2000 - 2007>
    32.         .Mask = oMask '<OFFICE 2002 - 2007>
    33.         .Visible = True
    34.     End With
    35.     'oCB.Reset '<TO RESET THE COMMANDBAR>
    36. End Sub
    Last edited by RobDog888; Jun 5th, 2007 at 08:25 PM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width