使 MS Word 中的自定义功能区 UI 无效

发布于 2025-01-12 08:59:13 字数 2156 浏览 3 评论 0原文

我在 MS Word 中制作了一个自定义选项卡,它构成文档检查过程的一部分。在该过程开始时,将打开一个用户表单,您可以添加 x 数量的电子邮件以供稍后在检查过程中使用。

这个想法是让自定义选项卡动态更新以显示相应的编号。在用户表单中输入电子邮件的按钮。

据我所知,我已经编辑了 XML 并拥有所有必需的功能,但是当我在用户表单完成后尝试调用 Reintialise() 以使 UI 无效时:

Public MyRibbon As IRibbonUI
Public Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef destination As Any, ByRef source As Any, ByVal length As Long)

Public Sub MyAddInInitialize(Ribbon As IRibbonUI)
    Set MyRibbon = Ribbon
    ActiveDocument.Variables.Add("RibbonAddress") = CStr(ObjPtr(Ribbon))
End Sub

Function GetRibbon(ByVal lRibbonPointer As LongPtr) As Object
    Dim objRibbon As Object
    CopyMemory objRibbon, lRibbonPointer, LenB(lRibbonPointer)
    Set GetRibbon = objRibbon
    Set objRibbon = Nothing
End Function

Sub btnCheck_getVisible(control As IRibbonControl, ByRef returnedVal)
    'If condition to toggle getVisible callback from VBA that works
End Sub

Sub Reintialise()
    If MyRibbon Is Nothing Then
        Set MyRibbon = GetRibbon(ActiveDocument.Variables("RibbonAddress"))
        MyRibbon.Invalidate
    Else
        MyRibbon.Invalidate
    End If
End Sub

我收到错误“运行时错误 '5825':运行此版本的代码(其中尝试将功能区信息写入变量)时,“对象已被删除”。如果在 sub retialise 中它只有 MyRibbon.Invalidate 我收到错误“运行时错误'91':未设置对象变量或块变量”,这显然是由于未定义 MyRibbon。

我试图调用 MyAddInIntialise 来重新定义对象 MyRibbon 并将“Ribbon”存储为文档变量以供稍后调用,但到目前为止我的方法都不起作用。

XML 按钮示例:

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="MyAddInIntialize">
    <ribbon>
        <tabs>
            <tab id="customTab" label="Checking" insertAfterMso="TabReviewWord">
                <group id="startGov" label="Governance Progress Overview">
                    <button id="emailCheck1" label="Send Document to First Checker" size="large" 
                    onAction="send_email" getVisible="btnCheck_getVisible" 
                    getEnabled="btn_trigger" imageMso="AppointmentColor1"/>
                </group>
            </tab>
        </tabs>
    </ribbon>

I have produced a custom tab in MS Word that forms part of a document checking process. At the beginning of the process, a userform is opened and you can add x amount of emails to be used later in the checking process.

The idea is for the custom tab to dynamically update to display the corresponding no. of buttons to emails entered in the userform.

I have edited the XML and have all required functions as far as I'm aware however when I try to call Reintialise() to invalidate the UI after the userform has been completed:

Public MyRibbon As IRibbonUI
Public Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef destination As Any, ByRef source As Any, ByVal length As Long)

Public Sub MyAddInInitialize(Ribbon As IRibbonUI)
    Set MyRibbon = Ribbon
    ActiveDocument.Variables.Add("RibbonAddress") = CStr(ObjPtr(Ribbon))
End Sub

Function GetRibbon(ByVal lRibbonPointer As LongPtr) As Object
    Dim objRibbon As Object
    CopyMemory objRibbon, lRibbonPointer, LenB(lRibbonPointer)
    Set GetRibbon = objRibbon
    Set objRibbon = Nothing
End Function

Sub btnCheck_getVisible(control As IRibbonControl, ByRef returnedVal)
    'If condition to toggle getVisible callback from VBA that works
End Sub

Sub Reintialise()
    If MyRibbon Is Nothing Then
        Set MyRibbon = GetRibbon(ActiveDocument.Variables("RibbonAddress"))
        MyRibbon.Invalidate
    Else
        MyRibbon.Invalidate
    End If
End Sub

I receive the error 'Run-time error '5825':' Object has been deleted' when running this version of the code where the ribbon information is attempted to be written to a variable. If in sub reintialise it just has MyRibbon.Invalidate I get the error 'Run-time error '91': Object Variable or With Block Variable not set' which is obviously from MyRibbon not being defined.

I have attempted to recall MyAddInIntialise to redefine the object MyRibbon as well as storing 'Ribbon' as a document variable to call later but none of my methods have worked thus far.

Example of XML button:

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="MyAddInIntialize">
    <ribbon>
        <tabs>
            <tab id="customTab" label="Checking" insertAfterMso="TabReviewWord">
                <group id="startGov" label="Governance Progress Overview">
                    <button id="emailCheck1" label="Send Document to First Checker" size="large" 
                    onAction="send_email" getVisible="btnCheck_getVisible" 
                    getEnabled="btn_trigger" imageMso="AppointmentColor1"/>
                </group>
            </tab>
        </tabs>
    </ribbon>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

も星光 2025-01-19 08:59:13

您是否遇到任何功能区 UI 错误?

默认情况下,如果加载项尝试操作 Microsoft Office 用户界面 (UI) 但失败,则不会显示错误消息。但是,您可以将 Microsoft Office 应用程序配置为显示与 UI 相关的错误消息。您可以使用这些消息来帮助确定为什么不显示自定义功能区,或者为什么显示功能区但不显示控件。

要显示 VSTO 外接程序用户界面错误,您需要:

  1. 启动应用程序。
  2. 单击文件选项卡。
  3. 单击选项
  4. 在类别窗格中,单击高级
  5. 在详细信息窗格中,选择显示 VSTO 外接程序用户界面错误,然后单击确定

请参阅 如何:显示加载项用户界面错误了解更多信息。


确保 Office 应用程序调用的 MyAddInInitialize 方法和对象已正确初始化。

Do you get any ribbon UI errors?

By default, if an add-in attempts to manipulate the Microsoft Office user interface (UI) and fails, no error message is displayed. However, you can configure Microsoft Office applications to display messages for errors that relate to the UI. You can use these messages to help determine why a custom ribbon does not appear, or why a ribbon appears but no controls appear.

To show VSTO Add-in user interface errors you need:

  1. Start the application.
  2. Click the File tab.
  3. Click Options.
  4. In the categories pane, click Advanced.
  5. In the details pane, select Show VSTO Add-in user interface errors, and then click OK.

See How to: Show Add-in user interface errors for more information.


Make sure your MyAddInInitialize method called by the Office application and the object is initialized correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文