1

I am trying to convert an "old" .Net-3.5-Project to .Net-4.0. By now everything works fine, but the Excel-Interop.

What I am having problems with is the opening of an workbook. I already tried an all new project and compiled it once with .Net-3.5 and once with .Net-4.0. With the "old" Framework it works as excpected, but with 4.0 I only get null as a result?

My code in my testapp is this:

using System;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Excel.Workbook test;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenWithInterop();
        }

        private void OpenWithInterop()
        {
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            excel.WorkbookOpen += new Excel.AppEvents_WorkbookOpenEventHandler(excel_WorkbookOpen);
            test = excel.Workbooks.Open(@"C:/Test/test.xlsx");

            excel.Quit();
        }

        void excel_WorkbookOpen(Excel.Workbook Wb)
        {
            if (test.Name.Equals(Wb.Name)) // Here there will be an null-exception with .net-4 but not with .net-3.5
            {
                Console.WriteLine("done it right");
            }
        }
    }
}

The testapp contains only a WinForm-Form (from the template) and I added just one button that has the above shown behaviour.

Is there anything I am missing here? Did anything change in the way to work with Excel in 4.0?

UPDATE: To answer your questions:

  • It's Office 2010 (x86) on Windows 7 Enterprise (x64)
  • I just added it in "references" in Visual Studio and called it as "using" in my Main-Class. (Will update my code to show it full)
  • That's now the real minimal code to show you the behaviour - strange isn't it?

UPDATE 2: I found out some "new" stuff:

  • It's also happening on Windows XP (x86)
  • You don't need to have the project, in which Excel.Interop is referenced, in .net4, it's enough that it is called by some .net4-project. Example:

BaseProject (.net-4) --> DataLayer (.net-3.5) references Excel.Interop --> Presentation (.net-4)

In this example the error will happen. Even when DataLayer is .net-3.5. BaseProject has to be .net-4, because it's calling Presentation which is .net-4 (and needs to be...)

UPDATE 3:

Just found out, that everything would work fine, but you run in really trouble if you are using one of these to events in .net4

excel.WorkbookBeforeClose += new Excel.AppEvents_WorkbookBeforeCloseEventHandler(this.HandleWorkbookClosed);
excel.WorkbookOpen += new Excel.AppEvents_WorkbookOpenEventHandler(this.HandleWorkbookOpen);

Are there any aquivalent events in Excel._Application like in Excel.ApplicationClass?

UPDATE 4:

To answer the question of the comments (thanks Will!) I extended the example a little bit. The main-problem is in the event. Why is there this difference in .net-4 or is somewhere documented? And how to avoid it?

basti
  • 2,649
  • 3
  • 31
  • 46
  • That works for me, btw you can leave off all the System.Type.Missings as they are optional arguments. How did you add the reference to the interop? – Dan May 04 '12 at 06:37
  • What office version are you using? – chris6523 May 04 '12 at 06:37
  • It's not clear - which variable is coming back as null? `excel` or `test`? – Will May 04 '12 at 11:13
  • It's interesting that this question `http://stackoverflow.com/questions/1922851/c-sharp-excel-interoperability` says we should use ApplicationClass, not Application. Does this help? – Will May 04 '12 at 11:15
  • @Will: I found out, that it's actually not coming back as null but as some strange type with Exceptions as values. I am talking about "test". The actual problem is described in Update-3. In the WorkbookOpen-event is a paramter Workbook given (the one that got opened). If you now compare this event-given-workbook with the one in "test" you will get strange exceptions. – basti May 04 '12 at 12:37
  • 1
    @Will: Your cited question has as toprated answer that you should change from ApplicationClass to Application – basti May 04 '12 at 12:37
  • Check for target framework whether you are using .net Framework 4.0 client profile or .net Framework 4.0 in project properties. – Romil Kumar Jain May 04 '12 at 13:12
  • Romil: I am using .net-Framework-4.0 - not the client-profile. – basti May 04 '12 at 13:40

1 Answers1

0

I have a kind of a solution by now:

I changed my 2 event-handlers from

    void excel_WorkbookOpen(Excel.Workbook Wb)
    {
        if (test.Name.Equals(Wb.Name)) // Here there will be an null-exception with .net-4 but not with .net-3.5
        {
            Console.WriteLine("done it right");
        }
    }

to this changed logic

    void excel_WorkbookOpen(Excel.Workbook wb)
    {
        if (!wb.FullName.Equals(pathToExcelFile)) // pathToExcelFile is class-wide visible
        {
            return;
        }

        Console.WriteLine("done it right");
    }

So this is a workaround to my previous logic.

As this is not a explanation why it works with 3.5 and not with 4 and it actually doesn't solve the problem I am not willing to accept my own answer as solution.

For future searches / problems of other people this thread might be helpful, so I will let it stay here open. If someone finds a explanation for the behaviour I would be absolute willing to accept it as an answer :)

basti
  • 2,649
  • 3
  • 31
  • 46