Rohan
Rohan

Reputation: 31

In a VSTO MS Word add-in, a Ribbon Toggle button clicked in one document causes it to be clicked in other running instances of MS Word

I have developed a MS Word add-in and it has several buttons in the ribbon menu. The first button is a Toggle button and clicking it causes a task pane to load. The problem I have been experiencing is that if I have multiple instances of MS Word open and I click this toggle button in one of the instances, it automatically appears clicked in the other running instances of MS Word as well but the task pane appears to be loaded only in the instance where I had clicked the toggle button. I would like the toggle button to work independently in every instance of MS Word. I have tried several ways of doing this but haven't found a solution yet. I have experienced the same behavior for another add-in that I developed for MS PowerPoint.

Any help in this matter will be appreciated.

I have a ribbon designer (named rbcOfficeAddin) added to my add-in project and it has a toggle button named btnTaskPane. Below is the code:

            public partial class rbcOfficeAddin
            {
                private void btnTaskPane_Click(object sender, RibbonControlEventArgs e)
                {
                    if (this.btnTaskPane.Checked)
                    { this.btnTaskPane.Label = "Hide Task Pane"; }
                    else { this.btnTaskPane.Label = "Show Hide Pane"; }
                    Globals.ThisAddIn.ShowHideActionPane(this.btnTaskPane.Checked);
                }
            }

The click handler of toggle button calls a method of ThisAddin as shown below:

            public partial class ThisAddIn
            {
                private bool operationsPaneCreated = false;

                public void ShowHideActionPane(bool flag)
                {
                    try
                    {
                        if (!this.operationsPaneCreated)
                        {
                            this.CreateTaskPane();
                            this.operationsPaneCreated = true;
                        }
                        myCustomTaskPane.Visible = flag;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }

                private void CreateTaskPane()
                {
                    //OperationsPane is a user control
                    oOperationsPane = new OperationsPane();
                    myCustomTaskPane = this.CustomTaskPanes.Add(oOperationsPane,
                        "Operations Pane");

                    myCustomTaskPane.DockPosition =
                        Office.MsoCTPDockPosition.msoCTPDockPositionFloating;
                    myCustomTaskPane.Height = 500;
                    myCustomTaskPane.Width = oOperationsPane.Width;

                    myCustomTaskPane.DockPosition =
                        Office.MsoCTPDockPosition.msoCTPDockPositionRight;
                    myCustomTaskPane.Width = 420;
                    myCustomTaskPane.Control.AutoScroll = true;

                    myCustomTaskPane.Visible = false;

                    myCustomTaskPane.VisibleChanged += myCustomTaskPane_VisibleChanged;
                }

                void myCustomTaskPane_VisibleChanged(object sender, EventArgs e)
                {
                    try
                    {
                        if (!myCustomTaskPane.Visible && Globals.Ribbons.rbcOfficeAddin.btnTaskPane.Checked)
                        {
                            Globals.Ribbons.rbcOfficeAddin.btnTaskPane.Checked = false;
                            Globals.Ribbons.rbcOfficeAddin.btnTaskPane.Label = "Show Task Pane";
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }

Upvotes: 2

Views: 1346

Answers (1)

Cindy Meister
Cindy Meister

Reputation: 25663

This same question was asked and answered on the MSDN forums. I'm copying/pasting the relevant information here, with attributions, from https://social.msdn.microsoft.com/Forums/vstudio/en-US/1c3cab02-b231-4453-ae09-325e025606bf/in-a-vsto-ms-word-addin-a-ribbon-toggle-button-clicked-in-one-document-causes-it-to-be-clicked-in?forum=vsto

Answer from Starain chen Microsoft contingent staff, Moderator:

As far as I know, this is by design, for an instance with multiple word documents, the same add-in will be in a domain, for example, a variable is changed will be affect other word documents if they are in the same instance. So, you need to deal with this scenario. (e.g. base on document name)

Answer from Cindy Meister, MVP, Moderator:

The following article explains the approach. It's valid for Ribbon customizations as well as just Custom Task Panes

https://msdn.microsoft.com/en-us/library/vstudio/bb608620

Upvotes: 1

Related Questions