Results 1 to 1 of 1

Thread: [FAQ's: OD] How do I assign a standard Office image to a menu item or toolbar button?

  1. #1

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

    [FAQ's: OD] How do I assign a standard Office image to a menu item or toolbar button?

    To assign a custom menu item or toolbar button a built in default image you need to obtain the FaceId number of the desired image first.

    Once you have your FaceId you can assign it to your menu item or toolbar button by setting its .FaceId property.

    The CommandBarButton FaceId numbers of the CommandBars collection in any Office app can be found easily using code like shown below.


    Word 2003 VBA Code Example:

    VB Code:
    1. Option Explicit
    2. 'Make sure you have Macros enabled
    3. Private Sub Document_Open()
    4.     On Error Resume Next
    5.     Dim oCB As CommandBar
    6.     Dim oCBB As CommandBarButton
    7.     Dim i As Integer
    8.     Dim ii As Integer
    9.     Open "C:\FaceIds.txt" For Output As #1
    10.         Print #1, Application.Name & " Command Bar Button FaceIds"
    11.         Print #1,
    12.         For i = 1 To Application.CommandBars.Count
    13.             Set oCB = Application.CommandBars(i)
    14.             For ii = 1 To oCB.Controls.Count
    15.                 Set oCBB = oCB.Controls(ii)
    16.                 Print #1, "Bar Name: [" & oCB.Name & "] Button Caption: [" & oCBB.Caption & "] ID: [" & oCBB.ID & "]"
    17.             Next
    18.             Set oCBB = Nothing
    19.         Next
    20.         Set oCB = Nothing
    21.     Close #1
    22.     'Optionally you can open the textfile
    23.     If Dir("C:\FaceIds.txt") <> vbNullString Then
    24.         Shell "C:\FaceIds.txt", vbMaximizedFocus
    25.     End If
    26. End Sub
    27.  
    28. 'Example Output File Contents:
    29. 'Microsoft Word Command Bar Button FaceIds
    30.  
    31. 'Bar Name: [Standard] Button Caption: [New &Blank Document] ID: [2520]
    32. 'Bar Name: [Standard] Button Caption: [&Open...] ID: [23]
    33. 'Bar Name: [Standard] Button Caption: [&Save] ID: [3]
    34. 'Bar Name: [Standard] Button Caption: [Permission (Unrestricted Access)] ID: [9004]
    35. 'Bar Name: [Standard] Button Caption: [&Mail Recipient] ID: [3738]
    36. 'Bar Name: [Standard] Button Caption: [&Print] ID: [2521]
    37. 'Bar Name: [Standard] Button Caption: [Print Pre&view] ID: [109]
    38. 'Bar Name: [Standard] Button Caption: [&Spelling and Grammar...] ID: [2566]
    39. 'Bar Name: [Standard] Button Caption: [&Research...] ID: [7343]
    40. 'Bar Name: [Standard] Button Caption: [Cu&t] ID: [21]
    41. 'Bar Name: [Standard] Button Caption: [&Copy] ID: [19]
    42. 'Bar Name: [Standard] Button Caption: [&Paste] ID: [22]
    43. '...
    44. '...
    45. '...
    Now that you have your list of FaceId numbers you can choose one for your custom menu item or toolbar button.

    Word 2003 VBA Code Example:

    VB Code:
    1. Option Explicit
    2.  
    3. Public WithEvents oCBBCustom As Office.CommandBarButton
    4.  
    5. Private Sub oCBBCustom_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    6.     MsgBox "Meow!!!"
    7. End Sub
    8.  
    9. Private Sub Document_Open()
    10.  
    11.     Dim oCB As Office.CommandBar
    12.     Dim oCBBTools As Office.CommandBarPopup
    13.  
    14.     Set oCB = Application.CommandBars("Menu Bar")
    15.     Set oCBBTools = oCB.Controls("&Tools")
    16.     Set oCBBCustom = oCBBTools.Controls.Add(msoControlButton, 1, , , True) 'Place our custom one at the bottom
    17.     With oCBBCustom                                                        'position (0 based)
    18.         .Caption = "Meow!"
    19.         .BeginGroup = True 'Add a line separator
    20.         .Enabled = True
    21.         .FaceId = 263 'Excel Image
    22.         .Visible = True
    23.     End With
    24.     'oCB.Reset 'To reset the menu.
    25. End Sub
    Last edited by RobDog888; Apr 27th, 2006 at 01:23 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