2

I am working on an addin for excel 2010 using C#. I have an existing worksheet that has some controls in it, namely a ComboBox. I am trying to write some code that will place a certain value in the combo box's text property, but I am having a hard time getting access to the control to do so.

The combo box is named 'ComboBox1' but if I try something like...

var combo = Controls["ComboBox1"];

I get an ArgumentOutOfRangeException.

Exploratory approaches to finding out what I am supposed to be doing aren't really providing useful information either. For example, if were to write;

MessageBox.Show(Controls[0].GetType())

The displayed message is 'NamedRangeImpl' which doesn't seem like a control at all. So my question is, how do I get access to the controls that are on my worksheet from my code?

A.R.
  • 15,405
  • 19
  • 77
  • 123
  • 1
    Did you add it as a form control or an ActiveX control? If it's an ActiveX control try `ActiveSheet.OLEObjects("ComboBox1").object.value = "foo"` – Banjoe Aug 22 '11 at 19:33
  • Good question. I didn't make the sheet, but I will find out what is what. – A.R. Aug 23 '11 at 11:16
  • You could try loop something like `foreach (Control c in Controls) MessageBox.Show(c.GetType().ToString());` and see what the output of that is. – Mark Lalor Aug 24 '11 at 18:30
  • @Mark, the type is mentioned in the question. They all come back as 'NamedRangeImpl' – A.R. Aug 25 '11 at 13:32

1 Answers1

1

I'm not exactly sure about the problem but I've made addins for word and if its like windows forms this should work nicely.

foreach (Control c in Controls) 
   if (c.Name == "comboBox1") {
      ComboBox box = (ComboBox)c;
      box.Items.Add("Thing added");
   }
Mark Lalor
  • 7,820
  • 18
  • 67
  • 106