Adding icons to ribbon

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,926
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I am trying to modify the Ribbon by adding my own custom functionality.

I have accomplished what I want using the CustomUI tool but if I want my buttons to be disabled initially, then enabled at runtime, I feel I must write the XML in VBA.

This is the code:

Code:
Sub LoadCustRibbon()

Dim hFile As Long
Dim path As String, fileName As String, ribbonXML As String, user As String

hFile = FreeFile
user = Environ("Username")
path = "C:\Users\" & user & "\AppData\Local\Microsoft\Office\"
fileName = "Excel.officeUI"

ribbonXML = "<mso:customUI      xmlns:mso='http://schemas.microsoft.com/office/2009/07/customui'>" & vbNewLine
ribbonXML = ribbonXML + "  <mso:ribbon>" & vbNewLine
ribbonXML = ribbonXML + "    <mso:qat/>" & vbNewLine
ribbonXML = ribbonXML + "    <mso:tabs>" & vbNewLine
ribbonXML = ribbonXML + "      <mso:tab id='reportTab' label='Reports' insertBeforeQ='mso:TabFormat'>" & vbNewLine
ribbonXML = ribbonXML + "        <mso:group id='reportGroup' label='Reports' autoScale='true'>" & vbNewLine
ribbonXML = ribbonXML + "          <mso:button id='runReport' label='PTO' "   & vbNewLine
ribbonXML = ribbonXML + "imageMso='AppointmentColor3'      onAction='GenReport'/>" & vbNewLine
ribbonXML = ribbonXML + "        </mso:group>" & vbNewLine
ribbonXML = ribbonXML + "      </mso:tab>" & vbNewLine
ribbonXML = ribbonXML + "    </mso:tabs>" & vbNewLine
ribbonXML = ribbonXML + "  </mso:ribbon>" & vbNewLine
ribbonXML = ribbonXML + "</mso:customUI>"

ribbonXML = Replace(ribbonXML, """", "")

Open path & fileName For Output Access Write As hFile
Print #hFile, ribbonXML
Close hFile

End Sub

Sub ClearCustRibbon()

Dim hFile As Long
Dim path As String, fileName As String, ribbonXML As String, user As String

hFile = FreeFile
user = Environ("Username")
path = "C:\Users\" & user & "\AppData\Local\Microsoft\Office\"
fileName = "Excel.officeUI"

ribbonXML = "<mso:customUI           xmlns:mso=""http://schemas.microsoft.com/office/2009/07/customui"">" & _
"<mso:ribbon></mso:ribbon></mso:customUI>"

Open path & fileName For Output Access Write As hFile
Print #hFile, ribbonXML
Close hFile

End Sub

and it was taken from this website:

Code:
https://stackoverflow.com/questions/8850836/how-to-add-a-custom-ribbon-tab-using-vba

I can disable my buttons simply by adding:

Code:
enabled='false'

What the website doesn't show is how to add icons.

I added this line:

Code:
image='createreport'

but where do I add the physical icon?

Is it possible and if so, how?

Thanks
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
If this is a ribbon update for one file, you should be putting ribbonX code into the file itself. For this you need the Office ribbonX editor which is a free download. Then use the getEnabled callback and an associated subroutine in your file to handle the enabled state of the button(s). See: Excel: Building an Excel add-in for some pointers.
 
Upvote 0
If this is a ribbon update for one file, you should be putting ribbonX code into the file itself. For this you need the Office ribbonX editor which is a free download. Then use the getEnabled callback and an associated subroutine in your file to handle the enabled state of the button(s). See: Excel: Building an Excel add-in for some pointers.
Thanks, I'll look into it.

If I understand you correctly, it's not advisable to write XML in the VB Editor, as in the website I showed?
 
Upvote 0
You are right. The method you mentioned above overwrites all ribbon customizations of your user. The one I refer to modifies the ribbon just for the workbook; so only when this workbook is opened, will the ribbon changes be displayed. ANy user-specific customizations are left untouched.
 
Upvote 0
You are right. The method you mentioned above overwrites all ribbon customizations of your user. The one I refer to modifies the ribbon just for the workbook; so only when this workbook is opened, will the ribbon changes be displayed. ANy user-specific customizations are left untouched.
Thanks for clarifying.
 
Upvote 0

Forum statistics

Threads
1,224,884
Messages
6,181,568
Members
453,053
Latest member
Kiranm13

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top