C# 成员';应用程序';关于嵌入式互操作类型';Microsoft.Office.Interop.Excel.Workbook';无法评估

C# 成员';应用程序';关于嵌入式互操作类型';Microsoft.Office.Interop.Excel.Workbook';无法评估,c#,excel,interop,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.Missin

大约在去年,我的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_范围行上抛出一个异常。例外情况如下:

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

我花时间研究一个可能的解决方案,包括;但是,如果更改Microsoft.Office.interop.Excel dll的嵌入互操作类型,仍然会出现相同的错误。我错过了什么明显的东西吗?有人有什么建议吗?TIA。

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

也许COM方法返回了未知值……可能是不同的office版本?是的,我想可能是COM出现了一些奇怪的问题;但是,如果我尝试导入以前工作正常的其他Excel文件,它仍然可以工作!所以我试图导入的这个Excel文件有点奇怪。当我查看公式->名称管理器时,我发现FBCKIDENT已正确定义,因此我仍然感到困惑…也许这会有所帮助,感谢您的反馈;但是,我不想将当前的实现更改为使用get_Address,除非我必须这样做,因为当前的实现适用于99.9%的导入文件。相反,我更想知道的是,我是否可以在Excel文件本身中检查一些东西,从而导致这个get_Range调用失败?它大约有33行。