0

we are using the Application.DocumentChange event so that when a document is loaded, it can check the name of the document, and then if it is named in a certain way show or hide buttons on the ribbon.

If I use the code below it works really well, it shows or hides the buttons correctly.

But when I run the addin in debug mode from Visual Studio, when the document is closing it gives this message.

System.Runtime.InteropServices.COMException: 'This command is not available because no document is open.'

As you can see I tried to put in an if statement to stop it running the code but it didnt work.

If I just install my addin, Word does not seem to crash or have any problems, maybe I shouldnt worry about it?

Thanks

    Private Sub Application_DocumentChange() Handles Application.DocumentChange

    If Globals.ThisAddIn.Application.Documents.Count > 0 Then

        If Globals.ThisAddIn.Application.ActiveDocument.BuiltInDocumentProperties("Title").Value = "Document Name Here" Then

            Globals.Ribbons.VisualfilesTab.AttachEvidence.Visible = True
        Else

            Globals.Ribbons.VisualfilesTab.GetAuthorisation.Visible = True

        End If

    End If

End Sub
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
Dan
  • 31
  • 1
  • 5

1 Answers1

0

Consider switching your custom UI to the ribbon XML over the designer-generated custom ribbon UI. You can export your existing custom UI to the ribbon XML markup. See How to: Export a ribbon from the Ribbon Designer to Ribbon XML for more information.

You can customize the Ribbon UI by using callback procedures in COM add-ins. For each of the callbacks that the add-in implements, the responses are cached.

For example, if an add-in writer implements the getVisible callback procedure for a button, the function is called once, the state is loaded, and then if the visibility state needs to be updated, the cached state is used instead of recalling the procedure. This process remains in place until the add-in signals that the cached values are invalid by using the Invalidate or IRibbonUI.InvalidateControl method, at which time, the callback procedure is again called and the return response is cached. The add-in can then force an immediate update of the UI by calling the Refresh method.

Read more about the the Fluent UI (aka Ribbon UI) in the following articles:

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45