ExcelAppObj is sometimes null

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

ExcelAppObj is sometimes null
 
MarcumLLP




Posts: 31
Joined: 2015-02-10
I have an event that gets fired from a textbox "TextChanged" event. The following line gets executed:

Excel._Application ExcelApp = ExcelAppObj as Excel._Application;

However, on few occasions the ExcelApp variable is null.

Is there something I should set first to ensure I dont get a null back?


Thank you,
Tom
Posted 03 Aug, 2017 10:01:32 Top
Andrei Smolin


Add-in Express team


Posts: 14106
Joined: 2006-05-11
Hello Tom,

Does this occur on an ADXExcelTaskPane? What build number of AddinExpress.XL.2005.DLL are you using? What Excel build are you using? Is it 32bit or 64bit? Is the event triggered by your code setting the textbox while constructing it? Does the issue occurs before an ADXExcelTaskPane.ADXBeforeTaskPaneShow event occurs?

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 03 Aug, 2017 10:07:17 Top
MarcumLLP




Posts: 31
Joined: 2015-02-10
Yes, its happening in ADXExcelTaskPane.

AddinExpress.XL.2005.DLL Version: 8.6.4408.2005
Office Version: Excel 2013 32-bit.

No, I am not setting the text when constructing it.
I am reading the text from the textbox in that TextChanged event.

I dont know if the issue occurs on the event ADXBeforeTaskPaneShow because though I do have code in that event, I am only setting the Visible property.

Thank you,
Tom
Posted 03 Aug, 2017 10:26:48 Top
Andrei Smolin


Add-in Express team


Posts: 14106
Joined: 2006-05-11
Tom,

Is there a call stack?

I'm sorry, I won't be able to respond to your questions today as it is past 6PM here. See you tomorrow.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 03 Aug, 2017 10:31:20 Top
MarcumLLP




Posts: 31
Joined: 2015-02-10
Here is the call stack: (I noticed typing this post to you that its not a NULL reference issue but a "Reference Not Valid")

Message: Reference is not valid.

StackTrace: at Microsoft.Office.Interop.Excel._Application.Goto(Object Reference, Object Scroll)
at K1_PlugIn_Excel.Panes.JumpBarPane.NavigateToSheet(String sheetName) in D:\SourceCollections\Integration\MarcumK1\branches\Release2\branch-Dev-Tom\K1.PlugIn.Excel\Panes\JumpBarPane.cs:line 361


Here is the sections of code pertaining to the stack:

private void NavigateToSheet(string sheetName)
{
Excel._Application ExcelApp = ExcelAppObj as Excel._Application;

#361 --> ExcelApp.Goto($"'{sheetName}'!R1C1", Missing.Value);

}

private void stripTextBoxJumpBar_TextChanged(object sender, EventArgs e)
{
stripTextBoxJumpBar.BackColor = System.Drawing.Color.White;

if (stripTextBoxJumpBar.Text.Length == 0)
return;

if (stripTextBoxJumpBar.Text.IndexOf(WORKSHEET_IDENITIFIER) > -1)
{
Caller --> NavigateToSheet(stripTextBoxJumpBar.Text.Replace(WORKSHEET_IDENITIFIER, ""));
}
}
Posted 03 Aug, 2017 10:45:26 Top
Andrei Smolin


Add-in Express team


Posts: 14106
Joined: 2006-05-11
Hello Tom,

Thank you.

The call stack and error message show that the issue occurs because of the value you pass to the ExcelApp.Goto() method.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 04 Aug, 2017 06:27:01 Top
MarcumLLP




Posts: 31
Joined: 2015-02-10
Ah, great catch. Im so trained in my mind when I see a reference error, I'm immediately thinking of a memory reference, when the "Reference is not valid" is a bad Excel reference.

Great, thank you!
Posted 04 Aug, 2017 08:21:51 Top
Andrei Smolin


Add-in Express team


Posts: 14106
Joined: 2006-05-11
No problem! Good luck with your project!

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 04 Aug, 2017 08:23:39 Top
MarcumLLP




Posts: 31
Joined: 2015-02-10
Which leads me to my next question. Is there a way to safely check if a reference is valid before calling the GoTo() funciton?
Posted 04 Aug, 2017 08:25:12 Top
Andrei Smolin


Add-in Express team


Posts: 14106
Joined: 2006-05-11
I would probably call Worksheet.Cells[{reference}] and check the Excel.Range returned.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 04 Aug, 2017 08:29:30 Top