Thursday, February 10, 2011

Accessing Excel.ComboBox from C#

Hi,

I have a combobox (the drop down list control) in a pre-existing Excel template. I can reference this combobox in VBA with Sheet10.ComboBox1.

How can I reference this through Excel Interop in C#?

What I am trying to do is populate this combobox/dropdown with C# and have it positioned where I want with the template.

Thanks in advance.

  • The VBA code to do it is below. Basically you need to access the Worksheet.Shapes collection to find the item that corresponds to your ComboBox (either by index or more realistically by name). Then traverse the properties OLEFormat -> Object -> Object, casting as appropriate. The C# code is very similar.

    Dim wks As Worksheet
    Dim objShape As Shape
    Dim objComboBox As ComboBox
    Dim objOleObject As Excel.OleObject
    
    Set wks = Sheet1
    Set objShape = wks.Shapes(1)
    ' or Set objShape = wks.Shapes("ComboBox1")
    Set objOleObject = objShape.OLEFormat.Object
    Set objComboBox = objOleObject.Object
    
    From Joe

0 comments:

Post a Comment