Search code examples
c#asp.net-mvcms-wordoffice-addinsribbon-control

Multiple Instances of Word Document Ribbon button disable in Word Addin Project


My task was to create a new document from a given word document and then I need to disable the custom ribbon button only in that newly created Word document ribbon. Not the active document consider here because it is getting switch when user switch it.

Currently I cannot get the new Word instance ribbon control from C# code. When I apply following, both documents are affected.

CustomRibbon ribbon = Globals.Ribbons.CustomRibbon;
ribbon.button.Enabled = false;

Solution

  • Something like this should work, you have to find a way to identify your document

    private void MyAddin_Startup(object sender, System.EventArgs a)
    {
        this.Application.DocumentChange += new ApplicationEvents4_DocumentChangeEventHandler(Application_DocumentChange);
    }
    
    private void Application_DocumentChange()
    {
        bool enableButton = false;
        if(yourdocument)   // put something here that checks the document you want the button to be enable in
        { 
            enableButton = true;
        }
        CustomRibbon ribbon = Globals.Ribbons.CustomRibbon;
        ribbon.button.Enabled = enableButton;
    }