Home > Enterprise >  Invalidate Custom Ribbon UI in MS Word
Invalidate Custom Ribbon UI in MS Word

Time:03-08

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" onl oad="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>

CodePudding user response:

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.

  • Related