none
How can I make a VSTO Office Ribbon's contents independant on a per document basis? RRS feed

  • Question

  • Good day.

    I have developed a VSTO Word Add-In.  It contains a ribbon (via designer) with various controls in it.  I need to ensure that each new document's instance of the ribbon does not impact any other.  For example;My ribbon has a dropdown/combobox on in.  If I update the contents of it via one document window...all the other contents are also changed.  I would imagine it is due to me using the Globals glass to access the ribbon, but I am not sure how to get around this.

    Any help would be greatly appreciated.

    Regards,

    Alan

    Tuesday, October 6, 2015 11:39 PM

Answers

  • Hi Alan,

    Based on my understanding, the ribbon customization in application-level Office solution initialized when the add-in is load first time. It means that all the ribbon UI share the source code in the Office solution, so it is expected when we change the ribbon in one document that will affect other documents in the same application process.

    If we want the ribbon's content is independent on document, we need to write custom code maintain the logic. Here is an simple demo for your reference:

    public partial class Ribbon1
    {
             public Dictionary<string, string> controlContext = new Dictionary<string, string>();
    
            public void SyncRibbonControl(string documentName)
            {
                string controlValue = "";
                if (controlContext.TryGetValue(documentName,out controlValue))
                {
                    Globals.Ribbons.Ribbon1.comboBox1.Text = controlValue;
                }
            }
    
            public void AddValue(string documentName,string controlValue)
            {
                string oldValue = "";
                if (controlContext.TryGetValue(documentName, out oldValue))
                    controlContext[documentName] = controlValue;
                else
                    controlContext.Add(documentName, controlValue);
            }
             
            private void button1_Click(object sender, RibbonControlEventArgs e)
            {
    
                AddValue(Globals.ThisAddIn.Application.ActiveDocument.Name, "1");
                SyncRibbonControl(Globals.ThisAddIn.Application.ActiveDocument.Name);
                    }
    }
    
    public partial class ThisAddIn
    {
      private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                this.Application.WindowActivate += Application_WindowActivate;
              
            }
    
            private void Application_WindowActivate(Word.Document Doc, Window Wn)
            {
               Globals.Ribbons.Ribbon1.SyncRibbonControl(Doc.Name);
             }
    
    }
    
    

    Hope it is helpful.

    Regards & Fei



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 7, 2015 3:25 AM
    Moderator

All replies

  • Hi Alan,

    Based on my understanding, the ribbon customization in application-level Office solution initialized when the add-in is load first time. It means that all the ribbon UI share the source code in the Office solution, so it is expected when we change the ribbon in one document that will affect other documents in the same application process.

    If we want the ribbon's content is independent on document, we need to write custom code maintain the logic. Here is an simple demo for your reference:

    public partial class Ribbon1
    {
             public Dictionary<string, string> controlContext = new Dictionary<string, string>();
    
            public void SyncRibbonControl(string documentName)
            {
                string controlValue = "";
                if (controlContext.TryGetValue(documentName,out controlValue))
                {
                    Globals.Ribbons.Ribbon1.comboBox1.Text = controlValue;
                }
            }
    
            public void AddValue(string documentName,string controlValue)
            {
                string oldValue = "";
                if (controlContext.TryGetValue(documentName, out oldValue))
                    controlContext[documentName] = controlValue;
                else
                    controlContext.Add(documentName, controlValue);
            }
             
            private void button1_Click(object sender, RibbonControlEventArgs e)
            {
    
                AddValue(Globals.ThisAddIn.Application.ActiveDocument.Name, "1");
                SyncRibbonControl(Globals.ThisAddIn.Application.ActiveDocument.Name);
                    }
    }
    
    public partial class ThisAddIn
    {
      private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                this.Application.WindowActivate += Application_WindowActivate;
              
            }
    
            private void Application_WindowActivate(Word.Document Doc, Window Wn)
            {
               Globals.Ribbons.Ribbon1.SyncRibbonControl(Doc.Name);
             }
    
    }
    
    

    Hope it is helpful.

    Regards & Fei



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 7, 2015 3:25 AM
    Moderator
  • Many thanks.

    I had a feeling that was the case...but was hoping otherwise!!

    I will look at this (finally) and implement in my solution.


    Have an inspired day!

    Tuesday, October 27, 2015 12:22 PM