G
granttrimble
Hi
Can anybody please assist me with this problem. I'm trying to retrieve
the values from text boxes via C# (2005) in an Excel (2003) spread
sheet. I have tried casting the OLEFormat.Object to an MSForms.Text box
(2.0) which was unsuccessfull:
Excel.ApplicationClass excelApplication = null;
Excel.Workbook excelWorkbook = null;
Excel.Worksheet excelWorkSheet = null;
try
{
excelApplication = new Excel.ApplicationClass();
excelWorkbook =
excelApplication.Workbooks.Open(FileName,
Type.Missing,Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
string strSheet = "Dealer Job Card";
excelWorkSheet =
(Excel.Worksheet)excelWorkbook.Worksheets.get_Item(strSheet);
string strTextBoxName = "txtAccountNo";
Excel.Shape oShapeName =
excelWorkSheet.Shapes.Item(strTextBoxName);
MSForms.TextBox excelTextBox = (MSForms.TextBox)
oShapeName.OLEFormat.Object;
string strText = excelTextBox.Text;
I get this exception:
Unable to cast COM object of type 'System.__ComObject' to interface
type 'Microsoft.Vbe.Interop.Forms.TextBox'. This operation failed
because the QueryInterface call on the COM component for the interface
with IID '{8BD21D13-EC42-11CE-9E0D-00AA006002F3}' failed due to the
following error: No such interface supported (Exception from HRESULT:
0x80004002 (E_NOINTERFACE)).
I have also tried this:
Excel.ApplicationClass excelApplication = null;
Excel.Workbook excelWorkbook = null;
Excel.Worksheet excelWorkSheet = null;
excelApplication = new Excel.ApplicationClass();
excelWorkbook = excelApplication.Workbooks.Open(FileName,
Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing);
string strSheet = "Dealer Job Card";
excelWorkSheet =
(Excel.Worksheet)excelWorkbook.Worksheets.get_Item(strSheet);
string strTextBoxName = "txtAccountNo";
Excel.Shape oShapeName =
excelWorkSheet.Shapes.Item(strTextBoxName);
Type t = oShapeName.OLEFormat.Object.GetType();
string strText = t.InvokeMember("Text",
BindingFlags.GetProperty, null,
oShapeName.OLEFormat.Object, null).ToString();
which doesn't want to work either. I get the following exception:
Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))
Can anybody please assist me with this problem. I'm trying to retrieve
the values from text boxes via C# (2005) in an Excel (2003) spread
sheet. I have tried casting the OLEFormat.Object to an MSForms.Text box
(2.0) which was unsuccessfull:
Excel.ApplicationClass excelApplication = null;
Excel.Workbook excelWorkbook = null;
Excel.Worksheet excelWorkSheet = null;
try
{
excelApplication = new Excel.ApplicationClass();
excelWorkbook =
excelApplication.Workbooks.Open(FileName,
Type.Missing,Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
string strSheet = "Dealer Job Card";
excelWorkSheet =
(Excel.Worksheet)excelWorkbook.Worksheets.get_Item(strSheet);
string strTextBoxName = "txtAccountNo";
Excel.Shape oShapeName =
excelWorkSheet.Shapes.Item(strTextBoxName);
MSForms.TextBox excelTextBox = (MSForms.TextBox)
oShapeName.OLEFormat.Object;
string strText = excelTextBox.Text;
I get this exception:
Unable to cast COM object of type 'System.__ComObject' to interface
type 'Microsoft.Vbe.Interop.Forms.TextBox'. This operation failed
because the QueryInterface call on the COM component for the interface
with IID '{8BD21D13-EC42-11CE-9E0D-00AA006002F3}' failed due to the
following error: No such interface supported (Exception from HRESULT:
0x80004002 (E_NOINTERFACE)).
I have also tried this:
Excel.ApplicationClass excelApplication = null;
Excel.Workbook excelWorkbook = null;
Excel.Worksheet excelWorkSheet = null;
excelApplication = new Excel.ApplicationClass();
excelWorkbook = excelApplication.Workbooks.Open(FileName,
Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing);
string strSheet = "Dealer Job Card";
excelWorkSheet =
(Excel.Worksheet)excelWorkbook.Worksheets.get_Item(strSheet);
string strTextBoxName = "txtAccountNo";
Excel.Shape oShapeName =
excelWorkSheet.Shapes.Item(strTextBoxName);
Type t = oShapeName.OLEFormat.Object.GetType();
string strText = t.InvokeMember("Text",
BindingFlags.GetProperty, null,
oShapeName.OLEFormat.Object, null).ToString();
which doesn't want to work either. I get the following exception:
Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))