-1

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;
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
KIS
  • 129
  • 1
  • 10
  • since word uses one instance for all documents you have to enable the button in the word document where it is needed, there should be an onactivate event or similar , and disable it when it loses the focus. – Thorarins Sep 15 '15 at 07:27
  • Thanks for Reply Thorarins, Can u explain it further with sample code? – KIS Sep 15 '15 at 08:29

1 Answers1

1

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;
}
Thorarins
  • 1,836
  • 16
  • 21
  • hi Thorarins, Yes that is good, but need to find a way to identify the second word instance. Do I need to set CustomProperty while generating that second document then how i access in ThisAddIn class? – KIS Sep 15 '15 at 10:26
  • you need to set something (a custom propertie) on the document that needs the button , and then you check for that custom property and if it is there just enable otherwise you just activated another document. – Thorarins Sep 15 '15 at 10:28