PDA

View Full Version : [SOLVED:] Button from Ribbon UI should call VBA function



Markus
12-14-2022, 07:15 AM
Hello All,
I have a button in Word via VBA that saves the Word document back to a SQL database.

Here is the button definition so far:


Set oCommandBar = CommandBars.Add(Name:="Exp", Position:=msoBarTop)
oCommandBar.Visible = True

Set oMenue = oCommandBar.Controls.Add(Type:=msoControlButton)
With oMenue
.Caption = "Speichern nach Exp ..."
.Style = msoButtonCaption
.OnAction = "SendToExp"
.Visible = True
End With

OnAction calls the function


Public Function SendToExp() As String
Dim httpreq As MSXML2.XMLHTTP30
Dim Data As String
Dim prop As DocumentProperty
Dim url As String
........
End Function

This has also worked flawlessly for years. Now I wanted to add a second button that also saves back a PDF.
Since I had to work on the macro anyway, I wanted to change the buttons to a modern ribbon UI.

New Ribbon UI XML


<customUI xmlns="Link office /2009/07/ customui">
<ribbon startFromScratch="false">
<tabs>
<tab id="customTab" label="Justus Tab">
<group id="customGroup" label="Justus Group">
<button id="customButton" label="Exp1" imageMso="FileSave" size="normal" onAction="Messagebox" />
<button id="customButton2" label="Exp2" imageMso="FileSave" size="normal" onAction="SendToExp" />
</group>
</tab>
</tabs>
</ribbon>
</customUI>

The button for the message box works:


Sub Messagebox(control As IRibbonControl)
MsgBox("Hallo")
End Sub

When pressing the SendToExp button, an error occurs:

"Incorrect number of arguments or invalid assignment of a property".


Public Function SendToExp(control As IRibbonControl) As String
......
End Function

Can someone tell me how the call must be directed?

Best thanks and many greetings

Markus

Paul_Hossler
12-14-2022, 09:05 AM
Callbacks have a very specific format

SendToExp MUST look like this since it's a Button callback. I usually use a global variable to hold any results, like file name.



Sub Messagebox(control As IRibbonControl)

Sub SendToExp(control As IRibbonControl)

Markus
12-15-2022, 12:33 AM
Thanks for your help, problem solved