none
distinguishing user selection among opened document windows RRS feed

  • Question

  • hi,

    in my Office Word VSTO  Add-In i've placed a check box on ribbon which is initially unchecked. After user selects it certain operations will be performed as assigned. Here, after it was checked, when the user open another new document  obviously it also has the Add-In loaded. But, as a result of the previous action the new document window's add-in check box checked itself. After the new window's check box un-checking  the earlier window's check box losing the selection. this is happening all the time no matter of the number of opened new document windows.

    Can anyone tell me what to do to overcome this problem. Solution like keeping the user selection specific to corresponding document window.


    Friday, January 15, 2016 11:05 AM

Answers

  • Hello Kareem,

    The Fluent UI cashes values for all windows. To get a separate value for each window you need to specify the getPressed callback (enables you to specify whether the checkBox control is pressed) in the ribbon XML markup and return the state for each window separately. You can read more about the Fluent UI (aka Ribbon UI) in the following series of articles in MSDN library:

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)

    Also you may find the following ones helpful:

    Chapter 11: Creating Dynamic Ribbon Customizations (1 of 2)

    Chapter 11: Creating Dynamic Ribbon Customizations (2 of 2)

    Note, the Ribbon Designer doesn't provide all features of the Fluent UI, so you will have to use Ribbon XML instead. You can easily export your custom UI into XML by right clicking on the designer surface and choosing the export option in the context menu. See Walkthrough: Creating a Custom Tab by Using Ribbon XML for more information. 

    Saturday, January 16, 2016 11:52 AM
  • Hi KAREEM,

    First of all I did not properly understand your this sentence, “how to know whether the check box is checked from within ribbon.cs file”. Means I am saying that it totally depends upon your code that what type of output you want to achive. The second thing you ask that how to make checkbox selected. Then I want to say you that by various way you can select checkbox to be checked. Like by setting a property of it or set to checked it by calling event on some control.

    You can check the following example of getpressed.

    Ribbon1.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
      <ribbon>
        <tabs>
          <tab idMso="TabAddIns">
            <group id="MyGroup"
                   label="My Group">
                    <checkBox id="checkbox1" enabled="true"
                      label="TestCheckbox"
                      
                      visible="true"
                      getPressed="chkTest_pressed"
                      onAction="OnAction" />
              <button id ="btnTest" size="large" label="TestButton" onAction="btnTest_Click"/>
            </group>
          </tab>
        </tabs>
      </ribbon>
    </customUI> 
    Ribbon1.cs
    namespace WordAddIn2
    {
        [ComVisible(true)]
        public class Ribbon1 : Office.IRibbonExtensibility
        {
            private Office.IRibbonUI ribbon;
            private bool isChecked = false;
            public Ribbon1()
            {
            }
            public void btnTest_Click(Office.IRibbonControl ribbon)
            {
                isChecked = true;
                this.ribbon.Invalidate();
            }
            public bool chkTest_pressed(Office.IRibbonControl ribbon)
            {
                return isChecked;
    
            }
            #region IRibbonExtensibility Members
    
            public string GetCustomUI(string ribbonID)
            {
                return GetResourceText("WordAddIn2.Ribbon1.xml");
            }
    
            #endregion
    
            #region Ribbon Callbacks
            //Create callback methods here. For more information about adding callback methods, visit http://go.microsoft.com/fwlink/?LinkID=271226
    
            public void Ribbon_Load(Office.IRibbonUI ribbonUI)
            {
                this.ribbon = ribbonUI;
            }
    
            #endregion
    
            #region Helpers
    
            private static string GetResourceText(string resourceName)
            {
                Assembly asm = Assembly.GetExecutingAssembly();
                string[] resourceNames = asm.GetManifestResourceNames();
                for (int i = 0; i < resourceNames.Length; ++i)
                {
                    if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i])))
                        {
                            if (resourceReader != null)
                            {
                                return resourceReader.ReadToEnd();
                            }
                        }
                    }
                }
                return null;
            }
    
            #endregion
        }
    }

    Regards,

    Edward


    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.


    Tuesday, January 19, 2016 7:09 AM

All replies

  • Hello Kareem,

    The Fluent UI cashes values for all windows. To get a separate value for each window you need to specify the getPressed callback (enables you to specify whether the checkBox control is pressed) in the ribbon XML markup and return the state for each window separately. You can read more about the Fluent UI (aka Ribbon UI) in the following series of articles in MSDN library:

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 1 of 3)

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 2 of 3)

    Customizing the 2007 Office Fluent Ribbon for Developers (Part 3 of 3)

    Also you may find the following ones helpful:

    Chapter 11: Creating Dynamic Ribbon Customizations (1 of 2)

    Chapter 11: Creating Dynamic Ribbon Customizations (2 of 2)

    Note, the Ribbon Designer doesn't provide all features of the Fluent UI, so you will have to use Ribbon XML instead. You can easily export your custom UI into XML by right clicking on the designer surface and choosing the export option in the context menu. See Walkthrough: Creating a Custom Tab by Using Ribbon XML for more information. 

    Saturday, January 16, 2016 11:52 AM
  • yet not finished, 

    i managed to set up the getPressed callback function, this approach threw few more problems to me. here, how to know whether the check box is checked from within ribbon.cs file, and how do i made the check box as selected programmatically...? 

    Monday, January 18, 2016 11:09 AM
  • Hello Kareem,

    how to know whether the check box is checked from within ribbon.cs file

    Your business logic should give you the answer. There is no need to rely on the UI control, it just reflects the state of your business objects. 

    how  do i made the check box as selected programmatically

    To refresh the state of Ribbon controls you may use the Invalidate or InvalidateControl method of the IRibbonUI interface. It forces the host application to invoke your callbacks for gathering new values and updating the UI according to them.




    Monday, January 18, 2016 11:17 AM
  • at last, to which type the IRibbonControl.Context can be cast to. in word, is i have to reference Outlook library additionally to cast this property to Inspector type or Word namespace have any type..? i need the window handle in which my ribbon control resides.

    thanks

     
    Tuesday, January 19, 2016 4:48 AM
  • Hi KAREEM,

    First of all I did not properly understand your this sentence, “how to know whether the check box is checked from within ribbon.cs file”. Means I am saying that it totally depends upon your code that what type of output you want to achive. The second thing you ask that how to make checkbox selected. Then I want to say you that by various way you can select checkbox to be checked. Like by setting a property of it or set to checked it by calling event on some control.

    You can check the following example of getpressed.

    Ribbon1.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
      <ribbon>
        <tabs>
          <tab idMso="TabAddIns">
            <group id="MyGroup"
                   label="My Group">
                    <checkBox id="checkbox1" enabled="true"
                      label="TestCheckbox"
                      
                      visible="true"
                      getPressed="chkTest_pressed"
                      onAction="OnAction" />
              <button id ="btnTest" size="large" label="TestButton" onAction="btnTest_Click"/>
            </group>
          </tab>
        </tabs>
      </ribbon>
    </customUI> 
    Ribbon1.cs
    namespace WordAddIn2
    {
        [ComVisible(true)]
        public class Ribbon1 : Office.IRibbonExtensibility
        {
            private Office.IRibbonUI ribbon;
            private bool isChecked = false;
            public Ribbon1()
            {
            }
            public void btnTest_Click(Office.IRibbonControl ribbon)
            {
                isChecked = true;
                this.ribbon.Invalidate();
            }
            public bool chkTest_pressed(Office.IRibbonControl ribbon)
            {
                return isChecked;
    
            }
            #region IRibbonExtensibility Members
    
            public string GetCustomUI(string ribbonID)
            {
                return GetResourceText("WordAddIn2.Ribbon1.xml");
            }
    
            #endregion
    
            #region Ribbon Callbacks
            //Create callback methods here. For more information about adding callback methods, visit http://go.microsoft.com/fwlink/?LinkID=271226
    
            public void Ribbon_Load(Office.IRibbonUI ribbonUI)
            {
                this.ribbon = ribbonUI;
            }
    
            #endregion
    
            #region Helpers
    
            private static string GetResourceText(string resourceName)
            {
                Assembly asm = Assembly.GetExecutingAssembly();
                string[] resourceNames = asm.GetManifestResourceNames();
                for (int i = 0; i < resourceNames.Length; ++i)
                {
                    if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i])))
                        {
                            if (resourceReader != null)
                            {
                                return resourceReader.ReadToEnd();
                            }
                        }
                    }
                }
                return null;
            }
    
            #endregion
        }
    }

    Regards,

    Edward


    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.


    Tuesday, January 19, 2016 7:09 AM