How to run a macro from button in custom ribbon ? - vb.net

I created a custom ribbon using Custom UI Editor For Microsoft Office
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
<ribbon>
<tabs>
<tab idMso="TabHome" >
<group id="customGroup1" label="My Group" insertAfterMso="GroupEditingExcel">
<button id="customButton1" label="Click Me" size="large" onAction="test()" imageMso="HappyFace" />
</group>
</tab>
</tabs>
</ribbon>
</customUI>
I saved excel 2007 file as Excel Macro-Enabled workbook
Here is my macro. It works fine when it run manually
Sub test()
For i = 1 To 10
For j = 1 To i
Cells(i, j) = j
Next
Next
End Sub
But When I press the smiling face button in my custom ribbon it gives following error message.
cannot run the macro 'test()' the macro may not be available in this
workbook or all macro may be disabled.
How can I fix this issue ? Any suggestions ?
Thank you

You should not include the brackets. So change this:
onAction="test()"
to:
onAction="test"
Also, make sure the routine name is unique and does not exist in any other workbook you might have open in Excel, otherwise you risk the wrong routine being called.

Related

VBA - Ribbon customization - When adding an add-on

I did a program that I wished to distribute to my co-workers, they are not VBA oriented. So I wish to distribute it easily.
I created an Add-in, when install the add-in needs to create a custom Ribbon. I tried my best, but I cannot find easy to understand documentation.
My code is as follow :
Private Sub Workbook_AddinInstall()
On Error Resume Next 'Just in case
Application.CommandBars("Worksheet Menu Bar").Controls("Super Code").Delete
Set cControl = Application.CommandBars("Worksheet Menu Bar").Controls.Add
With cControl
.Caption = "Open SCF workbook"
.Style = msoButtonIconAndCaptionBelow
.OnAction = "OpenTheCorrectFile"
.FaceId = 7720
.DescriptionText = "Open the SCF workbook"
End With
Set cControl = Application.CommandBars("Worksheet Menu Bar").Controls.Add
With cControl
.Caption = "Are they onboard"
.Style = msoButtonIconAndCaptionBelow
.FaceId = 5817
.OnAction = "Check_Suppliers_Already_On_Board"
.DescriptionText = "Check if suppliers have already been on boarded"
End With
End Sub
Now if you use my code you will notice that:
Buttons are small
Icons are small
No description when hoovering them
No special name for the new ribbon, it is call Add-ins
No special name for this group, it is call "Menu Commands"
Does anyone know how to solve this.
Whenever I am looking for those answer people are using special application to create the ribbons, I do not wish to do it. I am also a beginner in VBA, so everything that is easily digestible is welcome.
Thank you very much.
Here's an example of how you can do it without using any external application, just a text editor. But you will have to be careful to write XML code properly, or the excel file may not open correctly (make a backup copy).
You will create a folder named "customUI" and, inside that folder, a file named "customUI.xml"
You will write all your Ribbon details inside "customUI.xml". I wrote this example, covering the issues you listed: Big buttons and icons (Images list in https://bert-toolkit.com/imagemso-list.html) / Tooltip description (screentip and supertip) / Ribbon name (tab label) / Group names (group labels)
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
<ribbon>
<tabs>
<tab id="CustomTab" label="Add-ins">
<group id="Group1" label="Menu Commands">
<button id="Button1" label="Happy" size="large" screentip="Test1" supertip="Test1 description" imageMso="HappyFace" onAction="Macro1" />
<button id="Button2" label="Save" size="large" imageMso="FileSave" onAction="Macro2" />
<button id="Button3" label="{a}" size="large" imageMso="FieldCodes" onAction="Macro3" />
</group >
<group id="Group2" label="Other Commands">
<button id="Button4" label="Reply" size="large" imageMso="Reply" onAction="Macro4" />
</group >
</tab>
</tabs>
</ribbon>
</customUI>
Rename your .XLSM file to .ZIP, then you will be able to edit/add files. Add the customUI folder to the zip file.
From the ZIP file, extract the .rels file that is inside _rels folder, you will add this line between the last Relationship tag and the Relationships closing tag, and then put it back inside the ZIP file overwriting the .rels file there
<Relationship Id="someID" Type="http://schemas.microsoft.com/office/2006/relationships/ui/extensibility" Target="customUI/customUI.xml" />
Rename the file back to .XLSM and open it, if everything is right, the new Ribbon will be there. It's important that the macros that will be called by the buttons have a control parameter, like this:
Sub Macro3(ByVal control As IRibbonControl)
That's it, everything about the UI is in the customUI.xml file, there is no more adding buttons in VBA. If you have to change something in the UI, you will have to rename it to ZIP again, overwrite the customUI.xml file and rename to XLSM again. Here's the Ribbon I got in this example:

"The macro cannot be found or has been disabled because of your security settings." PPAM addin causing this error from Ribbon but not from Code

I have some VBA PPT macros along with custom ribbon interface. Whose macros work fine from code but when made into ppam addin and added to PPT addins, some commands throw this error msg, however the macro works well, just want to stop this annoying msgs. The subs and functions are all Public and it still shows the same error. Please help
Public Sub frmFeedNotes_show()
frmFeedNotes.Show
End Sub
<!--RibbonX Visual Designer 1.94 for Microsoft PowerPoint 16.0. XML Code produced on 2017-10-06-->
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" >
<ribbon >
<tabs >
<tab
id="zenTools"
label="Zen Tools"
visible="true">
<group
id="FileProp"
label="File Properties"
visible="true">
<menu id="mnuExport"
label="Export Slides"
showImage="true"
imageMso="ExportToVCardFile" size="large">
<button id="btnFeedNotes"
label="Feed Notes*"
imageMso="FootnotesEndnotesShow"
onAction="frmFeedNotes_show" />
<button id="btnNotesCSV"
label="Notes as CSV"
imageMso="CommaSign"
onAction="ExportNotes" />
</menu>
</group >
</tab >
</tabs >
</ribbon >
</customUI >
Need to insert module name for onAction e.g. Module1
<button id="btnNotesCSV"
label="Notes as CSV"
imageMso="CommaSign"
onAction="Module1.ExportNotes" />
The ribbon Button also will pass a parameter to the sub, so expect it to receive a parameter. Add a parameter for the macro as follow
Public Sub frmFeedNotes_show(ByVal control As IRibbonControl)
frmFeedNotes.Show
End Sub

How to combine multiple VBA macros into one add-in file for Excel

I have written some 4 to 5 macros for Excel 2010, now I want to add all of these macros into a Custom Tab in the Ribbon as buttons. But I want to install all the macros as one complete package (or add-in). I have successfully installed one macro by creating a CustomUI package with the help of Visual Studio, but I don't know how to add other macros to the same add-in.
It depends on where you've stored your macro. For instance if your macro is saved in a module (within the Add-in file), then you would add an event handler to call the function as specified in the CustomUI. For example I have two macros, one called 'PricingTaxable' and one called 'PricingPerformance'.
Events handlers in module:
Sub PricingTaxable_eventhandler(control As IRibbonControl)
PricingTaxable
End Sub
Sub PricingPerformance_eventhandler(control As IRibbonControl)
PricingPerformance
End Sub
And the CustomUI XML code (find the event handler and function names to know what to do):
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
<ribbon>
<tabs>
<tab id="customTab" label="Pricing" insertAfterMso="TabHome">
<group id="customGroup1" label="Taxable Income">
<button id="customButton1" label="Taxable Income" size="large"
onAction="PricingTaxable_eventhandler" imageMso="PivotTableLayoutReportLayout" />
</group>
<group id="customGroup2" label="Performance Sheets">
<button id="customButton2" label="Performance" size="large"
onAction="PricingPerformance_eventhandler" imageMso="ChartTrendline" />
</group>
</tab>
</tabs>
</ribbon>
</customUI>
Refer here for more help: https://erpcoder.wordpress.com/2012/05/30/how-to-create-a-custom-ribbon-addin-for-excel-2010/

MS Word and RibbonX error (Wrong number of arguments ...)

I am trying to add an extra tab to the Word ribbon and have a couple buttons to invoke my own macros. I did the same thing with Excel and it worked fine. For some reason I can't get it to work in Word. I am using the Custom UI Editor. Here's the ribbon code.
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
<ribbon startFromScratch="false">
<tabs>
<tab id="customTab" label="Pavel">
<group id="customGroup" label="Formatting">
<button id="customButton" label="Custom Button" imageMso="HappyFace" size="large" onAction="Callback" />
</group>
</tab>
</tabs>
</ribbon>
</customUI>
Just a very simple tab, with one button that calls the macro Callback().
I added a module to the Word file with the following code.
Sub Callback()
MsgBox ("Test")
End Sub
The macro runs fine on its one (hitting the play button in the editor) but when invoked via the button on the tab I get the following error:
Edit: It seems to do something with 1) public access to the module and 2) apparently the macro should be taking some argument similar to "control as ribboncontrol" or something like that. If you know where to find the documentation for this, please let me know.
A Ribbon button expects a Sub which takes a parameter:
Sub Callback(button As IRibbonControl)
MsgBox ("Test")
End Sub
http://msdn.microsoft.com/en-us/magazine/cc163469.aspx

VBA - Word 2007 - Subscript out of range (error 9)

I don't know what's causing this.
I've created a template with a lot of macro code in it. And I've used the Custom UI EDitor for Office 2007 applications to create a custom Tab in the Ribbon interface. In this tab I've got this button which is supposed to open a userform. The XML code for the button is this:
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
<ribbon startFromScratch="false">
<tabs>
<tab id="CustomForm" label="CustomForm">
<group id="customuserdata" label="Custom Userdata">
<button id="openForm" label="Open userform" imageMso="OpenForm" size="large" onAction="openForm" />
</group>
</tab>
</tabs>
</ribbon>
</customUI>
And in the same Template I've got a Public Sub named openForm and it this I just run myForm.Show. It's in this sub that the error occurs "Run-Time error '9'. SubScript out of range". What could cause this?
The exact code of the Sub I'm trying to call is this:
Public Sub openForm(control As IRibbonControl)
FormAltData.Show ' This is where the debugger stops at'
End Sub
The error was caused by an Array I believe. I tried to access an array before it was even declared ^^ My bad :)