简体   繁体   中英

Identify which ribbon control was clicked from a macro (MS Word VBA)

I have several macros that all do near identical things: each one opens a separate file. I have them activated through controls on a customized ribbon. But instead of having several macros that look like this:

ChangeFileOpenDirectory SeriesPath
Documents.Open FileName:="Doc1.docx", _
    ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
    PasswordDocument:=Password$, PasswordTemplate:="", Revert:=False, _
    WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
    wdOpenFormatAuto, XMLTransform:=""

but with only the filename changed, I'd like to have one macro that can open any one document depending on which ribbon element was clicked. Problem is, I need to know the ID of the ribbon element that was clicked. I've been through several web sites that all suggest using the IRibbonUI.ID property, but when I try that, I get an error message from Word: "Runtime error 91: Object variable or With block variable not set."

Here's a sample of the exported XML code for my ribbon:

<mso:button idQ="x1:Open_00_1_549FAC6" label="00" imageMso="BlackAndWhiteDontShow" onAction="Open_00" visible="true"/>
<mso:button idQ="x1:Open_01_10_549FAC6" label="01" imageMso="AppointmentColor0" onAction="Open_01" visible="true"/>
<mso:button idQ="x1:Open_02_9_549FAC6" label="02" imageMso="AppointmentColor1" onAction="Open_02" visible="true"/>
<mso:button idQ="x1:Open_03_8_549FAC6" label="03" imageMso="AppointmentColor2" onAction="Open_03" visible="true"/>

Does anyone know what I'm doing wrong?

First, please excuse any syntax errors, these answers are in psuedo vba/xml.

1) I would suggest using the id parameter of the ribbon control object to determine the callee, instead of using a different function. You could possibly rename them to something more clean such as:

idQ="x1:Open_00" label="00" ... onAction="Open_OnAction"
idQ="x1:Open_01" label="01" ... onAction="Open_OnAction"

2) You can then 'parse' based on the ID, or just do a case/switch statement on the entire ID

Sub Open_OnAction(control As IRibbonControl)
    Dim filename As String
    Select Case control.id
        Case "x1:Open_00"
            filename = "somefilename00.xml"
        Case "x1:Open_01"
            filename = "somefilename01.xml"
    End Select

    'The rest of your code here
End Sub

*Note: If you wanted to be even more slick, you could put filename into the ID (via XML), and not even use a case statement, and parse it out. EG:

idQ="Open_myfilenamepathhere" ... onAction="Open_OnAction"

Sub Open_OnAction(control As IRibbonControl)
    Dim filename As String

    'This splits the control into multiple array elements based on delimeter of "_", then grabs the 2nd element (since the array's first element is 0)
    filename = Split(control.id,"_")(1)

    'The rest of your code here
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM