none
How to get a reference to Ribbon.IRibbonUI in ThisDocument.cs for Invalidate()? RRS feed

  • Question

  • I am using VS2008 .net framework 3.5 to write a VSTO Document-Level solution in Word 2007. I have a problem in IRibbonUI.Invalidate(). I know how to do it in Ribbon.cs but I don't know how to get a reference to Ribbon to perform the method IRibbonUI.Invalidate(). Please help! I do some code as follow:

     

    Ribbon1.xml
    
    <?xml version="1.0" encoding="UTF-8"?>
    <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="Ribbon_Load">
      <commands>
        <command idMso="FontDialog" getEnabled="GetEnable"/>
        <command idMso="Font" getEnabled="GetEnable"/>
        <command idMso="ParagraphDialog" getEnabled="GetEnable"/>
        <command idMso="BulletsGalleryWord" getEnabled="GetEnable"/>
        <command idMso="NumberingGalleryWord" getEnabled="GetEnable"/>
        <command idMso="MultilevelListGallery" getEnabled="GetEnable"/>
        <command idMso="StylesPane" getEnabled="GetEnable"/>
        <command idMso="QuickStylesGallery" getEnabled="GetEnable"/>
        <command idMso="ChangeStylesMenu" getEnabled="GetEnable"/>
        <command idMso="CoverPageInsertGallery" getEnabled="GetEnable"/>
        <command idMso="BlankPageInsert" getEnabled="GetEnable"/>
        <command idMso="PageBreakInsertWord" getEnabled="GetEnable"/>
        <command idMso="HeaderInsertGallery" getEnabled="GetEnable"/>
        <command idMso="FooterInsertGallery" getEnabled="GetEnable"/>
        <command idMso="HeaderFooterPageNumberInsert" getEnabled="GetEnable"/>
      </commands>
     
      <ribbon>
        <tabs>
          <tab id="Testing" label="Testing">
            <group id="GetEnable" label="GetEnable">
             <button id="go" label="GO!" size="large" onAction="OnAction" />
            </group>
          </tab>
        </tabs>
      </ribbon>
    </customUI>
    
    Ribbon1.cs
    
    namespace Testing
    {
      [ComVisible(true)]
      public class Ribbon1 : Office.IRibbonExtensibility
      {
        private Office.IRibbonUI ribbon;
        private bool enable;
    
        public RibbonLDD()
        {
          enable = false;
        }
    
        #region IRibbonExtensibility Members
    
        public string GetCustomUI(string ribbonID)
        {
          return GetResourceText("Testing.Ribbon1.xml");
        }
    
        #endregion
    
        #region Ribbon Callbacks
        //Create callback methods here. For more information about adding callback methods, select the Ribbon XML item in Solution Explorer and then press F1
    
        public void Ribbon_Load(Office.IRibbonUI ribbonUI)
        {
          this.ribbon = ribbonUI;
        }
    
        public void GetEnable(Office.IRibbonControl control)
        {
          return enable;
        }
    
        public void OnAction(Office.IRibbonControl control)
        {
          enable = true;
    
          this.ribbon.Invalidate();
        }
    
        public void SetEnable(bool en)
        {
          enable = en;
        }
    
        #endregion
      }
    }
    
    ThisDocument.cs
    
    namespace Testing
    {
      public partial class ThisDocument
      {
        private void ThisDocument_Startup(object sender, System.EventArgs e)
        {
        }
    
        private void ThisDocument_Shutdown(object sender, System.EventArgs e)
        {
        }
    
        #region VSTO Designer generated code
    
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
          this.Startup += new System.EventHandler(ThisDocument_Startup);
          this.Shutdown += new System.EventHandler(ThisDocument_Shutdown);
          this.New += new Microsoft.Office.Interop.Word.DocumentEvents2_NewEventHandler(ThisDocument_New);
        }
    
        private void ThisDocument_New()
        {
          // How to get the reference to Ribbon1???
          Ribbon1.SetEnable(true);
          Ribbon1.IRibbonUI.Invalidate();
        }
    
        #endregion
    
        protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
        {
          return new Ribbon1();
        }
      }
    }
    

    Thursday, September 2, 2010 1:22 PM

Answers

  • Hi Totoro

    <<...but I don't know how to get a reference to Ribbon to perform the method IRibbonUI.Invalidate().>>

    If you were using the Ribbon Designer, it would be available to you via Globals.Ribbons.RibbonClassName

    However, in VS2008 with RibbonXML this is not the case. I solve it as follows, where I declare a static field for the class that holds the Ribbon object. I assign the object in the onLoad callback for the customUI element. Note that, since you're changing the way VSTO is doing this internally that you also have to set the ribbon object for the class, itself. In ThisAddin, then I can call Ribbon1.solutionRibbon.Invalidate();

      public class Ribbon1 : Office.IRibbonExtensibility
      {
        private Office.IRibbonUI ribbon;
        internal static Office.IRibbonUI solutionRibbon;<br/>
    <br/><br/><br/><br/>    #region IRibbonExtensibility Members
    
        public string GetCustomUI(string ribbonID)
        {
          return GetResourceText("WordAddin_RibbonXML.Ribbon1.xml");
        }
    
        public void Ribbon_Load(Office.IRibbonUI ribbonX)
        {
          ribbon = ribbonX;
          solutionRibbon = ribbonX;
        }
    

     


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Totoro2007 Saturday, September 4, 2010 1:26 AM
    Thursday, September 2, 2010 4:07 PM
    Moderator
  • Hi,

    Sorry, that was a solution if you used the Ribbon Visual Designer. 

    What if you store the reference of the new RibbonUI you create for the add-in?

    Ribbon1 myRibbon = null;
    protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
      {
        myRibbon = new Ribbon1();
       return myRibbon;
      }

    or instead of CreateRibbonExtensibilityObject,

        Ribbon1 ribbon;
        protected override object RequestService(Guid serviceGuid)
        {
          if (serviceGuid == typeof(Office.IRibbonExtensibility).GUID)
          {
            if (ribbon == null)
              ribbon = new Ribbon1();
            return ribbon;
          }
    
          return base.RequestService(serviceGuid);
        }

    Hope it helps.

    Mauro.

    • Marked as answer by Totoro2007 Saturday, September 4, 2010 1:25 AM
    Thursday, September 2, 2010 3:16 PM

All replies

  • Hi,

    Try this:

      private void ThisDocument_New()
      {
       Globals.Ribbons.Ribbon1.SetEnable(true);
        Globals.Ribbons.Ribbon1.IRibbonUI.Invalidate();
      }

    Regards,

    Mauro

     

    Thursday, September 2, 2010 2:30 PM
  • Hi,

    Try this:

     

     private void ThisDocument_New()
     {
      Globals.Ribbons.Ribbon1.SetEnable(true);
       Globals.Ribbons.Ribbon1.IRibbonUI.Invalidate();
     }

    Regards,

    Mauro

     

     

    hi, I found that it have no Ribbon1 under Globals.Ribbons...the code cannot be compile.

    I have no idea how to get the Ribbon1 object in ThisDocument.cs....

    Thursday, September 2, 2010 2:39 PM
  • Hi,

    Sorry, that was a solution if you used the Ribbon Visual Designer. 

    What if you store the reference of the new RibbonUI you create for the add-in?

    Ribbon1 myRibbon = null;
    protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
      {
        myRibbon = new Ribbon1();
       return myRibbon;
      }

    or instead of CreateRibbonExtensibilityObject,

        Ribbon1 ribbon;
        protected override object RequestService(Guid serviceGuid)
        {
          if (serviceGuid == typeof(Office.IRibbonExtensibility).GUID)
          {
            if (ribbon == null)
              ribbon = new Ribbon1();
            return ribbon;
          }
    
          return base.RequestService(serviceGuid);
        }

    Hope it helps.

    Mauro.

    • Marked as answer by Totoro2007 Saturday, September 4, 2010 1:25 AM
    Thursday, September 2, 2010 3:16 PM
  • Hi,

    Sorry, that was a solution if you used the Ribbon Visual Designer. 

    What if you store the reference of the new RibbonUI you create for the add-in?

     

    Ribbon1 myRibbon = null;
    protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
     {
      myRibbon = new Ribbon1();
      return myRibbon;
     }

    or instead of CreateRibbonExtensibilityObject,

      Ribbon1 ribbon;
      protected override object RequestService(Guid serviceGuid)
      {
       if (serviceGuid == typeof(Office.IRibbonExtensibility).GUID)
       {
        if (ribbon == null)
         ribbon = new Ribbon1();
        return ribbon;
       }
    
       return base.RequestService(serviceGuid);
      }

    Hope it helps.

    Mauro.

     

    hi, thanks your help, I can get the ribbon object using your code but I cannot call the method IRibbonUI.Invalidate().

    how can I call the method IRibbonUI.Invalidate()?

    I had tried something like this but fail...

    Ribbon1.cs
    
    namespace Testing
    {
      [ComVisible(true)]
      public class Ribbon1 : Office.IRibbonExtensibility
      {
        private Office.IRibbonUI ribbon;
        private bool enable;
    
        public Ribbon1()
        {
          enable = false;
        }
    
        #region IRibbonExtensibility Members
    
        public string GetCustomUI(string ribbonID)
        {
          return GetResourceText("Testing.Ribbon1.xml");
        }
    
        #endregion
    
        #region Ribbon Callbacks
        //Create callback methods here. For more information about adding callback methods, select the Ribbon XML item in Solution Explorer and then press F1
    
        public void Ribbon_Load(Office.IRibbonUI ribbonUI)
        {
          this.ribbon = ribbonUI;
        }
    
        public void GetEnable(Office.IRibbonControl control)
        {
          return enable;
        }
    
        public void SetEnable(bool en)
        {
          enable = en;
        }
    
        public void InvalidateRibbon()
        {
          this.ribbon.Invalidate();
        }
    
        #endregion
      }
    }
    
    

     

    ThisDocument.cs
    
    namespace Testing
    {
      public partial class ThisDocument
      {
        Ribbon1 myRibbon;
    
        private void ThisDocument_Startup(object sender, System.EventArgs e)
        {
        }
    
        private void ThisDocument_Shutdown(object sender, System.EventArgs e)
        {
        }
    
        #region VSTO Designer generated code
    
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
          this.Startup += new System.EventHandler(ThisDocument_Startup);
          this.Shutdown += new System.EventHandler(ThisDocument_Shutdown);
          this.New += new Microsoft.Office.Interop.Word.DocumentEvents2_NewEventHandler(ThisDocument_New);
        }
    
        private void ThisDocument_New()
        {
          myRibbon.SetEnable(true);
          myRibbon.InvalidateRibbon();
        }
    
        #endregion
    
        protected override object RequestService(Guid serviceGuid)
        {
          if (serviceGuid == typeof(Office.IRibbonExtensibility).GUID)
          {
            if (myRibbon == null)
              myRibbon = new Ribbon1();
    
            return myRibbon;
          }
    
          return base.RequestService(serviceGuid);
        }
      }
    }
    
    

    Thursday, September 2, 2010 3:43 PM
  • Hi,

    What error you get? Maybe the problem is that the Ribbon is not loaded when you want to invalidate it.

    May I ask you why do you want to invalidate the Ribbon when creating a new doc? Which is the requirement?

    Regards,

    Mauro.

    Thursday, September 2, 2010 3:49 PM
  • Hi,

    What error you get? Maybe the problem is that the Ribbon is not loaded when you want to invalidate it.

    May I ask you why do you want to invalidate the Ribbon when creating a new doc? Which is the requirement?

    Regards,

    Mauro.

    A error "A first chance exception of type 'System.NullReferenceException' occurred in Testing.DLL". Word2007 prompt a messagebox "The customization assembly could not be found or could not be loaded."

    I put the code in ThisDocument_New() just for testing. The requirement is to check who are using the template and lock some word2007's functions for difference user.

    Also, for difference document will lock some Word2007's functions also.

    Thursday, September 2, 2010 4:02 PM
  • Hi Totoro

    <<...but I don't know how to get a reference to Ribbon to perform the method IRibbonUI.Invalidate().>>

    If you were using the Ribbon Designer, it would be available to you via Globals.Ribbons.RibbonClassName

    However, in VS2008 with RibbonXML this is not the case. I solve it as follows, where I declare a static field for the class that holds the Ribbon object. I assign the object in the onLoad callback for the customUI element. Note that, since you're changing the way VSTO is doing this internally that you also have to set the ribbon object for the class, itself. In ThisAddin, then I can call Ribbon1.solutionRibbon.Invalidate();

      public class Ribbon1 : Office.IRibbonExtensibility
      {
        private Office.IRibbonUI ribbon;
        internal static Office.IRibbonUI solutionRibbon;<br/>
    <br/><br/><br/><br/>    #region IRibbonExtensibility Members
    
        public string GetCustomUI(string ribbonID)
        {
          return GetResourceText("WordAddin_RibbonXML.Ribbon1.xml");
        }
    
        public void Ribbon_Load(Office.IRibbonUI ribbonX)
        {
          ribbon = ribbonX;
          solutionRibbon = ribbonX;
        }
    

     


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Totoro2007 Saturday, September 4, 2010 1:26 AM
    Thursday, September 2, 2010 4:07 PM
    Moderator
  • Hi,

    Did you solve the issue?

    Regards,

    Mauro

    Friday, September 3, 2010 9:38 PM
  • Hi,

    Did you solve the issue?

    Regards,

    Mauro

    Yes.

    Thank you very much.

    Saturday, September 4, 2010 1:25 AM