简体   繁体   English

确定从宏中单击了哪个功能区控件(MS Word VBA)

[英]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. 问题是,我需要知道单击的功能区元素的ID。 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." 我曾经浏览过几个建议使用IRibbonUI.ID属性的网站,但是当我尝试这样做时,我从Word中收到一条错误消息:“运行时错误91:对象变量或未设置块变量。”

Here's a sample of the exported XML code for my ribbon: 这是我的功能区导出的XML代码的示例:

<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. 首先,请原谅任何语法错误,这些答案在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. 1)我建议使用功能区控制对象的id参数来确定被调用者,而不要使用其他函数。 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 2)然后,您可以根据ID进行“解析”,或者仅对整个ID进行case / switch语句

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. *注意:如果您想更加精致,可以将文件名(通过XML)放入ID中,甚至不使用case语句,然后将其解析出来。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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