0

I have a Ribbon1 class which was created by the Visual Studio.

public Microsoft.Office.Tools.Ribbon.RibbonEditBox IDBox;

I would like to access one of the ribbon controls from within a different class.

using MyRibbon = ExcelAddIn1.Ribbon1;

xlTextID = MyRibbon.IDBox.Text;

I got the error message.

An object reference is required for non-static field, method, property.

I can't make IDBox static because it's initialized as an instance of a class via 'InitializeComponent()' method.

this.IDBox = this.Factory.CreateRibbonEditBox();

I have also tried to create a property.

    private Microsoft.Office.Tools.Ribbon.RibbonEditBox IDBox;

    public Microsoft.Office.Tools.Ribbon.RibbonEditBox IDBoxProperty
    {
        get { return IDBox; }
        set { IDBox = value; }
    }

Doing this I have seen exactly the same error.

How can I keep IDBox non-static and still access it from outside class?

I can found the answer - see it below.

LLaP
  • 2,528
  • 4
  • 22
  • 34
  • 1
    possible duplicate of [An object reference is required for the nonstatic field, method, or property 'WindowsApplication1.Form1.setTextboxText(int)](http://stackoverflow.com/questions/498400/an-object-reference-is-required-for-the-nonstatic-field-method-or-property-wi) – David Arno Jun 11 '15 at 08:06
  • 1
    As per many near-identical questions here on the "An object reference is required for non-static field, method, property", you are accessing the class as if it were an instance (object) of that class. Rather than `MyRibbon.IDBox.Text`, you need to create an instance of `MyRibbon.IDBox` (or, more like, access the exisiting instance created by the `InitializeComponent` method. – David Arno Jun 11 '15 at 08:09
  • How do you access instances created by InitializeComponent method? – LLaP Jun 11 '15 at 08:13
  • That completely depends on where you are trying to access it from. – David Arno Jun 11 '15 at 08:16
  • I'm trying to access it from an external class. Ribbon1.cs lives in the main folder of the project. Then I have a folder called 'Controllers'. Inside this folder there is a class called 'Publishing'. I'm trying to access the Ribbon instance from within 'Publishing' class. – LLaP Jun 11 '15 at 08:19

1 Answers1

1

Instances of the all Ribbon controls derived from Microsoft.Office.Tools.Ribbon can be accessed via Globals.Ribbons.Ribbon1.

Therefore, in order to access public Microsoft.Office.Tools.Ribbon.RibbonEditBox IDBox; which is created by InitializeComponent()method, you would do Globals.Ribbons.Ribbon1.IDBox.

More information about accessing Ribbon Controls at Run-Time:

https://msdn.microsoft.com/en-us/library/bb772088.aspx

LLaP
  • 2,528
  • 4
  • 22
  • 34