简体   繁体   English

Excel COM对象强制转换错误

[英]Excel COM Object Cast Error

I'm trying to avoid "double dot" notation in my code (see How do I properly clean up Excel interop objects? ). 我试图在代码中避免使用“双点”表示法(请参阅如何正确清理Excel互操作对象? )。 However, I get an error when I try to change the following code 但是,当我尝试更改以下代码时出现错误

Excel.Workbook oWB;
...
oWB.Sheets[oWB.Sheets.Count].Delete();
oWB.Sheets[oWB.Sheets.Count].Delete();
oWB.Sheets[oWB.Sheets.Count].Delete();

to this 对此

Excel.Sheets oS;
oS = oWB.Sheets;
//error occurs in the following line
oS = oWB.Sheets[oS.Count];
oS.Delete();
oS.Delete();
os.Delete();

The error is 'Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Excel.Sheets'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D7-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).' 错误是'Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Excel.Sheets'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D7-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).' 'Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Excel.Sheets'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D7-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).'

Any help is appreciated. 任何帮助表示赞赏。

You have to declare a new variable of type Sheet . 您必须声明一个Sheet类型的新变量。 oS is of type Sheets , while oS.Sheets[os.Count] is of type Sheet (without final 's'). oS的类型为Sheets ,而oS.Sheets[os.Count]的类型为Sheet (不带最终的s)。 As these are unrelated type from the runtime point of view, you must declare an intermediate variable with appropriate type Sheet . 因为从运行时的角度来看它们是不相关的类型,所以必须声明一个具有适当类型Sheet的中间变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无效的强制转换异常,无法将COM对象强制转换为Excel - Invalid Cast Exception, Unable to Cast COM object to Excel 无法转换COM对象 - 错误:不支持此类接口 - Unable to cast COM object - error: No such interface supported 如何将COM对象类型转换为Excel。 - how to cast COM object type to Excel.Checkbox type C#: some of machines are throwing following error: Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' - C#: some of machines are throwing following error: Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' 无法投射 COM object - Unable to cast COM object 无法将com对象转换为控件 - Cannot cast com object to Control 强制将COM对象强制转换为接口 - Force cast a COM object to an interface 无法转换“Microsoft.Office.Interop.Excel.WorksheetClass”类型的COM对象 - Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.WorksheetClass' 无法将“System._ComObject”类型的 COM object 转换为“microsoft.Office.Interop.Excel.Application”” - unable to cast COM object of type 'System._ComObject' to 'microsoft.Office.Interop.Excel.Application'" 无法转换COM对象'(Microsoft.Office.Interop.Excel._Worksheet)workbook.Sheets' - Unable to cast COM object '(Microsoft.Office.Interop.Excel._Worksheet)workbook.Sheets'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM