Word add-in - ribbon

695 views Asked by At

I've create two buttons in my Word add-in ribbon : button1 and button2

when I open a doc of type 1 I just display the button1 and same thing when I open a second type of doc I just display button2.

the problem is when I open for example the first document, and I leave it open, and After I open the second document with the second type I always find the button1 in the ribbon.

how can I force the second document to display button2 and not button1 even if the first document is still open.

3

There are 3 answers

0
Kiru On

During the document change event invalidate(refresh) the ribbon.

private Office.IRibbonUI ribbon;
public void Ribbon_Load(Office.IRibbonUI ribbonUI)
{
Globals.ThisAddIn.Application.DocumentChange += DocumentChangeEvent;
}

private void DocumentChangeEvent()
{
ribbon.Invalidate();
}
0
Eugene Astafiev On

You need to define the getVisible callback in the ribbon XML markup of your custom UI. Then when required you can update your controls (actually force Word to invoke your callbacks) by calling the InvalidateControl or Invalidate method of the IRibbonUI interface.

You may consider using the WindowActivate or DocumentChange events of the Application class for checking whether the ribbon controls should be update. If so, call the Invalidate* methods. Do not call these methods every time.

To minimize the impact on performance, use the InvalidateControl method instead of the Invalidate method unless you actually need to invalidate all the custom controls or menu items that your add-in defines. Calling Invalidate invalidates all controls and menu items that your add-in defines, and callbacks occur on open controls and menus.

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

0
MSA On

You should invalidate ribbon on Window Activate

public Office.IRibbonUI ribbon;

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
   this.Application.WindowActivate += new Word.ApplicationEvents4_WindowActivateEventHandler(DocumentActivate);
}

private void DocumentActivate(Word.Document doc, Word.Window win)
{
   ribbon.Invalidate();
}