VSTO: Enable ribbon button only when a document is loaded

前端 未结 3 1949
遇见更好的自我
遇见更好的自我 2021-01-22 21:58

How does one set a ribbon button in a Word add-in to be enabled when a document is loaded and disabled when no documents are loaded, just Like most of the built-in buttons?

3条回答
  •  温柔的废话
    2021-01-22 22:50

    Use the DocumentChange event instead. Hook up will be something like this:

    Globals.ThisAddIn.Application.DocumentChange += new EventHandler(OnDocumentChange);
    

    And the Handler

    void OnDocumentChange()
    {
        this.myButton.Enabled = wordApp.Documents.Count > 0;
    }
    

提交回复
热议问题