C# How to read/update activex combobox from an Excel Macro-Enabled Workbook file (xlsm)

0

I am currently developing a C# application that opens, reads and updates an Excel Macro-Enabled Workbook file (xlsm). I was able to read and update cells but I was not able to read/update an ActiveX Control, specifically a Combobox.

using Excel = Microsoft.Office.Interop.Excel;

using MSForm = Microsoft.Vbe.Interop.Forms;

I tried different recipes based on research (http://forums.asp.net/t/1244356.aspx/1/10) for trying to read the selected item:

Excel.Shape aShape = xlWorkSheet.Shapes.Item(name);

Object o = aShape.OLEFormat.Object;

MSForm.ComboBox newBox = (MSForm.ComboBox)(((Excel.OLEObject)o).Object);

or alternatively

Excel.OLEObject cb1 = (Excel.OLEObject)xlWorkSheet.OLEObjects(name);

MSForm.ComboBox combo1 = (MSForm.ComboBox)cb1.Object;

but when I try to run the code I get the following error:

System.InvalidCastException : Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Vbe.Interop.Forms.ComboBox'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{8BD21D33-EC42-11CE-9E0D-00AA006002F3}' failed due to the following error: Interface not registered (Exception from HRESULT: 0x80040155).

Here are the details of my environment:

  • Windows 7 64bit
  • MS Visual Studio 2008 Professional Edition
  • .NET 3.5 SP1
  • Microsoft Excel 14.0.6129.5000 (32-bit)

As project references, I added the .NET components:

Path: C:\Program Files (x86)\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office12\Microsoft.Office.Interop.Excel.dll

Runtime Version: v1.1.4322

Version: 12.0.0.0

Path: C:\Program Files (x86)\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office11\Microsoft.Vbe.Interop.Forms.dll

Runtime Version: v1.1.4322

Version: 11.0.0.0

Any ideas on what could be the reason? If you already know how to change the selected item from the combobox please share it :)

Thank you!

c#
excel
com
interop
activex
asked on Stack Overflow Aug 8, 2013 by overflowed

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0