Excel._Application.ActiveWorkbook sometimes can be null

Add-in Express™ Support Service
That's what is more important than anything else

Excel._Application.ActiveWorkbook sometimes can be null
In my particular event WorkbookBeforeClose ActiveWorkbook might be null 
MarcumLLP




Posts: 31
Joined: 2015-02-10
I have this line of code in my event:
string fileName = (HostApplication as Excel._Application).ActiveWorkbook.FullName;

Sometimes ActiveWorkbook is null, however the link below talks about this as a possibility and recommends to call Activate(), but I dont see that method when using Add-In express.

https://social.msdn.microsoft.com/Forums/vstudio/en-US/78f303f5-e67a-427b-8898-1f79cbf0d7da/why-is-excelapplicationactiveworkbook-null?forum=csharpgeneral

How can I activate it so the ActiveWorkbook is no longer null?

Thanks
Posted 09 Feb, 2017 11:55:10 Top
Andrei Smolin


Add-in Express team


Posts: 14138
Joined: 2006-05-11
Hello,

MarcumLLP writes:
Sometimes ActiveWorkbook is null,


ActiveWorkbook is null if you start Excel, and close the default workbook (Workbook1). There's nothing to activate in this situation.

private void adxRibbonButton1_OnClick(object sender, IRibbonControl control, bool pressed) {
    Excel._Workbook wbk = ExcelApp.ActiveWorkbook;
    if (wbk == null)
        MessageBox.Show("ExcelApp.ActiveWorkbook is null");
    else
        MessageBox.Show("ExcelApp.ActiveWorkbook is NOT null");
}


The page that you refer to describes some other scenario.

As to the Activate method, search for it in the Excel object model; there are several objects providing it. Actually, if you have a Workbook object, you can call Workbook.Activate() - if this makes sense.

I'm sorry I won't be able to respond today: it is now 8 PM here.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 09 Feb, 2017 12:15:21 Top
MarcumLLP




Posts: 31
Joined: 2015-02-10
Thats odd, because more then 90% of the time ActiveWorkbook is not null on close, even when I only have one workbook open.
Posted 09 Feb, 2017 12:57:56 Top
Andrei Smolin


Add-in Express team


Posts: 14138
Joined: 2006-05-11
MarcumLLP writes:
is not null on close


In what event? What is your scenario and Excel version used? Make sure all other COM add-ins are turned off.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 10 Feb, 2017 03:10:32 Top
MarcumLLP




Posts: 31
Joined: 2015-02-10
Its on the event WorkbookBeforeClose. We are running Excel 2013, we do have a number of other COM AddinIns running, maybe that is the cause of the inconsistent behavior.
Posted 10 Feb, 2017 09:30:27 Top
Andrei Smolin


Add-in Express team


Posts: 14138
Joined: 2006-05-11
I remember issues related to this event. I'm pretty sure they didn't relate to getting null when accessing ActiveWorkbook but who knows. Make sure you have latest updates installed on Office 2013.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 10 Feb, 2017 10:12:34 Top