簡體   English   中英

無法評估嵌入式互操作類型“ Microsoft.Office.Interop.Excel.Workbook”上的成員“應用程序”

[英]Member 'Application' on embedded interop type 'Microsoft.Office.Interop.Excel.Workbook' cannot be evaluated

大約在過去的一年中,我的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類型無法嵌入 但是,如果更改Microsoft.Office.Interop.Excel dll的嵌入式互操作類型,仍然會出現相同的錯誤。 我是否缺少明顯的東西? 有沒有人有什么建議? TIA。

請注意,我最終通過手動將數據復制到新的Excel文件中並導入該文件解決了該問題-我仍然不確定原始Excel文件到底出了什么問題。 謝謝大家的建議。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM