1
Vote

NullReferenceException on ExcelApi.Application.GetActiveInstances()

description

Hi,

calling
ExcelApi.Application.GetActiveInstances()
with an open Excel instance which has no workbooks visibly opened (i.e. start excel and close or hide the default workbook) causes a NullReferenceException.


I tracked it down to the excel hot fix in GetActiveProxiesFromROT and the subsequent proxy lookup by hWnd.
If no workbook is visible there seems to be no EXCEL7 window and the lookup returns a null reference, which then causes the NullReferenceException on COMObject instantiation.

Best regards,
Andreas

comments

SebastianDotNet wrote Nov 13, 2015 at 4:50 PM

thanks for the notification. the main problem behind is: you can see all COM instance in ROT but only the first is accessible. This is why GetActiveObject in VB6/VBA has the same problem. You can't decide which instance you want and i've been implemented the hotfix.

Is it NetOffice 1.7.3 ?

I want change the implementation this weekend.

*Sebastian

SebastianDotNet wrote Nov 15, 2015 at 6:42 PM

BTW: do you have a stack trace, exception details(error code line number, etc.) for me?Its more safe to pin point the (real)problem once and for all.

*Sebastian

LockeLink wrote Dec 1, 2016 at 8:13 PM

Hi Sebastain,

I ran into this same problem today using NetOffice 1.7.3. I was able to find the 'GetActiveProxiesFromROT ' that Andreas mentioned. I created a brand new project, downloaded the NetOffice ExcelApi and put the following code in and it errors as they described.

For anyone else that runs into this problem the code shows how I get around the problem using the NetOffice.RunningObjectTable class.

Even with this issue NetOffice is a great library that I use a lot! Thanks!

-Locke
using System.Linq

class Program
    {
        static void Main(string[] args)
        {
            using (var application = new NetOffice.ExcelApi.Application())
            using (var application2 = new NetOffice.ExcelApi.Application())
            {
                application.Visible = true;
                application2.Visible = true;

                application.Workbooks.Add();
                application2.Workbooks.Add();

                //OK
                var applications = NetOffice.ExcelApi.Application.GetActiveInstances();

                foreach (var wb in application2.Workbooks)
                    wb.Close(false);

                //OK, though it will be missing "application2" because no workbooks are open.
                applications = NetOffice.RunningObjectTable
                    .GetActiveProxiesFromROT("Excel", "Application")
                    .Where(p => p != null)
                    .Select(p => new NetOffice.ExcelApi.Application(null, p))
                    .ToArray();

                //Not OK, this will error
                var applicationsError = NetOffice.ExcelApi.Application.GetActiveInstances();

                application.Quit();
                application2.Quit();
            }
        }
    }