This project has moved and is read-only. For the latest updates, please go here.

NullReferenceException on 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,


SebastianDotNet wrote Nov 13, 2015 at 5: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.


SebastianDotNet wrote Nov 15, 2015 at 7: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.


LockeLink wrote Dec 1, 2016 at 9: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!

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;


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

                foreach (var wb in application2.Workbooks)

                //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))

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