Unable to use interopexcel in my projet vs2015 with framework > 4.5

-1

I have a project in VS2015, I have Office 365 installed and references to Microsoft.Interop.Excel version 16.0. I have to detect active workbooks, list the sheets and import data. Here's a simple code for testing :

using Excel = Microsoft.Office.Interop.Excel;
...
private Microsoft.Office.Interop.Excel.Application oExcelApp;
private Microsoft.Office.Interop.Excel._Workbook oWorkBook;
private Microsoft.Office.Interop.Excel._Worksheet oSheet;
...
private void button1_Click(object sender, EventArgs e)
    oExcelApp = new Microsoft.Office.Interop.Excel.Application();
    try
    {
        oExcelApp = Microsoft.Office.Interop.Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
    }
    catch (Exception ex)
    {
        MessageBox.Show("Exception error in excelapp " + ex.Message);
    }
    string f = "";
    try
    {
        oWorkBook = oExcelApp.ActiveWorkbook;
        f += "active workbook " + oWorkBook.Name + "\r\n";
        oSheet = oWorkBook.ActiveSheet;
        f += "active workbook " + oWorkBook.Name + " active sheet " + oSheet.Name + "\r\n";
    }
    catch (Exception ex)
    {
        MessageBox.Show("Exception error in oWorkBook " + ex.Message);
    }
    MessageBox.Show(f);
    oExcelApp = null;            
}

An exception occurs at line oExcelApp.ActiveWorkbook. It's null.

When I browse into oExcelApp I can see a lot of errors like

{System.Reflection.TargetInvocationException: Une exception a été levée par la cible d'un appel. ---> System.Runtime.InteropServices.COMException: Ancien format ou bibliothèque de types non valide. (Exception de HRESULT : 0x80028018 (TYPE_E_INVDATAREAD))

I was sure that all was working fine but this week I had to make changes. Now I encounter errors like impossible to use workbook properties, active worksheet etc ... Null exception, Reference d'objet pas définie ...

I have repaired Office 365, I've seen the ticket unable to cast COM object of type 'microsoft.Office.Interop.Excel.ApplicationClass' to 'microsoft.Office.Interop.Excel.Application'" and thought that I had found the solution, but not. I have also tried Microsoft.Office.Interop.Excel doesn't work on 64 bit, to fix a problem with langage pack but same issue.

I have seen that if I create the projet with FrameWork 4.5, it works. If I change to FW 4.6 or higher, I have the problem. If I go back to 4.5, the issue remains.

Any idea ?
Thanks in advance for your attention ! Dominique

c#
visual-studio-2015
excel-interop
asked on Stack Overflow Sep 27, 2020 by Dom • edited Sep 27, 2020 by Dom

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0