如何使用 VBA 读取在自定义功能区选项卡上的编辑框中输入的文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11761907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:07:59  来源:igfitidea点击:

How to use VBA to read text entered in an editBox on a custom Ribbon tab?

vbaribbonms-projectribbonx

提问by Jeremy

I have added a tab to the Ribbon in MS Project 2010 using the SetCustomUI VBA method as detailed here: http://msdn.microsoft.com/en-us/library/ee767705.

我使用 SetCustomUI VBA 方法在 MS Project 2010 中的功能区中添加了一个选项卡,详情如下:http: //msdn.microsoft.com/en-us/library/ee767705

The tab XML loads fine and the layout is set up properly. The problem is when executing the VBA callbacks associated with my buttons and editBoxes, Project does not seem to be using the standard method signatures defined here: http://msdn.microsoft.com/en-us/library/aa722523.aspx#a16c7df5-93f3-4920-baa8-7b7290794c15_Ribbon.

选项卡 XML 加载良好,布局设置正确。问题是在执行与我的按钮和编辑框关联的 VBA 回调时,项目似乎没有使用此处定义的标准方法签名:http: //msdn.microsoft.com/en-us/library/aa722523.aspx#a16c7df5 -93f3-4920-baa8-7b7290794c15_Ribbon

These signatures work in the "more standard" Office programs like Excel 2010, but when I write the methods with the same parameters in Project, I get exception pop-ups unless I remove all parameters from the method. This is okay for buttons (which are demonstrated in the first MSDN article), but for the "onChange" callback for editBoxes, with no arguments provided in the callback, I have no way to read what text has been entered in the editBox, making them useless. How is the content of a Ribbon editBox supposed to be accessed in Project? Has VBA access to this String just been overlooked in Project? Can it only be accessed with Managed Code?

这些签名在 Excel 2010 等“更标准”的 Office 程序中有效,但是当我在 Project 中编写具有相同参数的方法时,除非我从方法中删除所有参数,否则我会收到异常弹出窗口。这对于按钮(在第一篇 MSDN 文章中演示)是可以的,但是对于 editBoxes 的“onChange”回调,在回调中没有提供参数,我无法读取在 editBox 中输入的文本,使得他们没用。应该如何在 Project 中访问 Ribbon editBox 的内容?项目中是否忽略了 VBA 对这个字符串的访问?只能使用托管代码访问吗?

Here's an example of an XML / VBA pair I've tried:

这是我尝试过的 XML/VBA 对的示例:

<customUI  xmlns="http://schemas.microsoft.com/office/2006/01/customui">
    <ribbon >
        <tabs >
            <tab 
                id="NavHelper"
                label="Nav Helper">
                <group 
                    id="Group1"
                    label="Search">
                    <box  id="Box1" >
                        <editBox 
                            id="DateBegin"
                            label="Dates from"
                            sizeString="11/11/1111"
                            onChange="DateBegin_onChange"/>
                        <editBox 
                            id="DateEnd"
                            label="to"
                            sizeString="11/11/1111"
                            onChange="DateEnd_onChange"/>
                    </box >
                    <button 
                        id="doSearch"
                        imageMso="InstantSearch"
                        label="Search"
                        size="large"
                        onAction="doSearch_onAction"/>
                </group >

            </tab >
        </tabs >
    </ribbon >
</customUI >

And the VBA:

和 VBA:

Public Sub DateBegin_onChange(control As IRibbonControl, text As String)
    MsgBox text
End Sub

Public Sub doSearch_onAction(control As IRibbonControl)
    MsgBox "pressed!"
End Sub

So with this example, the set up works perfectly in Excel, but if I bring it to Project, the callbacks won't run unless I remove all of the parameters from the method definitions, at which point they run, but are useless. Any ideas?

所以在这个例子中,设置在 Excel 中完美运行,但如果我将它带到 Project,除非我从方法定义中删除所有参数,否则回调将不会运行,此时它们运行,但无用。有任何想法吗?

采纳答案by Olle Sj?gren

Have you tried the Custom UI Editor for Microsoft Office?

您是否尝试过 Microsoft Office 的自定义 UI 编辑器?

See http://openxmldeveloper.org/blog/b/openxmldeveloper/archive/2009/08/06/7293.aspx

http://openxmldeveloper.org/blog/b/openxmldeveloper/archive/2009/08/06/7293.aspx

I haven't worked in MS Project, but in the other office programs you can enter the customUI XML and validate it and also get the corresponding VBA-function signatures.

我没有在 MS Project 中工作过,但是在其他办公程序中,您可以输入 customUI XML 并对其进行验证,还可以获得相应的 VBA 函数签名。

If I try your customUI XML in Excel and try to validate (I don't have Project...), I get an error saying the namespace is wrong.

如果我在 Excel 中尝试您的 customUI XML 并尝试验证(我没有 Project...),我会收到一条错误消息,指出命名空间错误。