无法评估嵌入式互操作类型“Microsoft.Office.Interop.Excel.Workbook”上的成员“应用程序”

时间:2013-07-03 22:24:16

标签: c# excel interop

在过去的一年里,我的C#代码在从Excel文件导入数据时运行良好:

Microsoft.Office.Interop.Excel.Application objExcel = new Microsoft.Office.Interop.Excel.Application();
            objExcel.Visible = false;
            objExcel.Workbooks.Open(TemplateFile, 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);
Microsoft.Office.Interop.Excel.Workbook TemplateWorkbook
Microsoft.Office.Interop.Excel.Range exRngFBCKIdent = null;
....

                foreach (Microsoft.Office.Interop.Excel.Worksheet objWorksheet in objExcel.ActiveWorkbook.Worksheets) //Loop through worksheets.
                {
                    // ...
                    try
                    {
                        exRngFBCKIdent = objWorksheet.get_Range("FBCKIDENT", Type.Missing);
                        ((Microsoft.Office.Interop.Excel._Worksheet)exRngFBCKIdent.Worksheet).Activate();
                    }
                    catch (Exception excException)
                    {
                        MessageBox.Show("ERROR: Range 'FBCKIDENT' was not found in the file - ensure this range is defined correctly and try again!", "Feedback Import", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return null;
                    }
...
}

然后突然对于特定的Excel文件,它开始在objWorksheet.get_Range代码行上抛出异常。这是一个例外:

objWorksheet.get_Range("FBCKIDENT", Type.Missing)   'objWorksheet.get_Range("FBCKIDENT", Type.Missing)' threw an exception of type 'System.Runtime.InteropServices.COMException'    Microsoft.Office.Interop.Excel.Range {System.Runtime.InteropServices.COMException}
base    {"Exception from HRESULT: 0x800A03EC"}  System.Runtime.InteropServices.ExternalException {System.Runtime.InteropServices.COMException}

如果我尝试快速监视objExcel对象,则会出现以下错误:

Member 'Application' on embedded interop type 'Microsoft.Office.Interop.Excel.Workbook' cannot be evaluated while debugging since it is never referenced in the program. Consider casting the source object to type 'dynamic' first or building with the 'Embed Interop Types' property set to false when debugging

我花时间研究可能的解决方案,包括Interop type cannot be embedded;但是,如果我更改Microsoft.Office.Interop.Excel dll的嵌入式互操作类型,我仍会得到相同的错误。我错过了一些明显的东西吗有没有人有什么建议? TIA。

1 个答案:

答案 0 :(得分:0)

请注意,我最终通过手动将数据复制到新的Excel文件并导入该文件来解决问题 - 我仍然不确定原始Excel文件到底出了什么问题。感谢大家的建议。