简体   繁体   中英

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

Over approximately the last year my C# code has worked fine for importing data from Excel files:

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;
                    }
...
}

Then suddenly for a particular Excel file, it started throwing an exception on the objWorksheet.get_Range line of code. Here is the exception:

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}

If I try to do a quick watch on the objExcel object, I get the following error:

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

I have spent time researching for a possible solution, including Interop type cannot be embedded ; however, I still get the same error if I change the embedded interop type for the Microsoft.Office.Interop.Excel dll. Am I missing something obvious? Does anyone have any suggestions? TIA.

Please note that I ended up solving the problem by manually copying the data over into a new Excel file and importing that file--I'm still not sure what exactly was wrong with the original Excel file. Thank you to everyone for your suggestions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM