How to Add a Custom Office Ribbon Button with VBA and CustomUI XML
Question details
The user wants to add a custom button to the Office Ribbon that executes a VBA macro upon being clicked.

- Product
- Microsoft Office / WPS Office
- Device & OS
- not provided
- Scenario
- Customizing the Office user interface to include specific macro-driven features for improved workflow.
- Observed behavior
- VBA cannot directly create or modify custom Office Ribbon tabs at runtime. A CustomUI XML part is required to successfully add tabs, groups, and buttons that call VBA procedures.
Ensure your workbook or document is saved in a macro-enabled format (like .xlsm or .docm) and keep a backup copy before making any manual edits to the file's XML structure.
Use CustomUI XML to Define the Ribbon Button
Modify the document's XML to build the Ribbon layout and use an onAction callback to connect the new button to your VBA code.
Because Office applications load the Ribbon interface when a file is opened, you cannot generate Ribbon elements purely using standard VBA during runtime. You must explicitly define the interface using Open XML.
To achieve this, you need a specialized tool like the Office RibbonX Editor or an Open XML editor to safely inject the CustomUI part into your document package.
Open your macro-enabled document, press ALT + F11 to open the VBA Editor, and insert a new Standard Module. Write a public procedure that takes the IRibbonControl parameter, such as: Public Sub RunMyMacro(control As IRibbonControl).
Close your Office document. Open the file in an Open XML editor (such as Office RibbonX Editor). Insert a Custom UI XML part (either Office 2007 or 2010+ format depending on your target version).
In the XML editor, write the XML structure to define your custom interface. Ensure you include the 'onAction' attribute in your button tag and set it to the exact name of your VBA procedure (e.g., <button id="CustomButton1" label="Run Macro" onAction="RunMyMacro" />).
Save the XML changes and close the editor. Reopen your macro-enabled document in your Office application. Your custom tab will appear on the Ribbon; click the button to execute your VBA code.

Run Custom VBA Macros Easily with WPS Office
WPS Office fully supports VBA macros and perfectly renders custom Ribbon UI elements embedded within XML, allowing you to execute your custom scripts without changing any code.
- 1. Download WPS Office: Install the latest version of WPS Office on your device.
- 2. Enable Macros: Navigate to the Developer tab in WPS Office and ensure that macros are enabled for your session.
- 3. Open Your XML-Modified File: Open your .xlsm or .docm file containing the CustomUI XML. WPS Office will automatically read the XML and display your custom Ribbon tabs.
- 4. Run the Custom Macro: Click your newly added custom Ribbon button to seamlessly trigger the attached VBA callback procedure.

Frequently Asked Questions
Can I use VBA to add a Ribbon button without XML?
No. The modern Office Ribbon architecture prevents standard VBA from dynamically creating or modifying Ribbon tabs and groups at runtime. The interface must be statically defined using CustomUI XML when the file loads.
What does the onAction attribute do in CustomUI XML?
The onAction attribute serves as the bridge between your XML interface and your code. It tells the application exactly which VBA macro (callback procedure) to run when a user clicks the specified Ribbon button.
Why is my custom Ribbon tab not showing up?
This is usually caused by XML syntax errors (like unclosed tags), incorrect case sensitivity in attributes, or saving the document in a non-macro format (like .xlsx instead of .xlsm). Validate your XML and ensure macro settings are enabled.
Do I need special software to edit the CustomUI XML?
While technically you can rename a file to .zip and edit the underlying XML files with Notepad, it is highly recommended to use a dedicated tool like the Office RibbonX Editor, which manages the folder structure and relationship IDs automatically.




