At which point during an Excel XLL startup is the HostApplication available?

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

At which point during an Excel XLL startup is the HostApplication available?
 
Jason Rodman




Posts: 40
Joined: 2016-11-01
Following the generated code template for an Excell XLL addin, I am trying to grab an instance of the excel application to use in future calls. I tried reading the generated "ExcelApp" property in both the OnInitialize and OnOpen events, but when I read it, excel's interface immediately shows and it prompts to save the current document. Once I click thru that prompt, the HostApplication property is null. What is its proper use? I can't find documentation on it anywhere.
Posted 17 Apr, 2017 16:24:07 Top
Andrei Smolin


Add-in Express team


Posts: 18844
Joined: 2006-05-11
Hello Jason,

Hmmm, I've reproduced the issue. We are looking into it. When I know anything about it, I'll let you know.


Andrei Smolin
Add-in Express Team Leader
Posted 18 Apr, 2017 05:22:38 Top
Andrei Smolin


Add-in Express team


Posts: 18844
Joined: 2006-05-11
Jason,

You can use ExcelApp from a UDF. Alternatively, you can get it earlier using this trick:

        private void XLLModule_OnInitialize(object sender, EventArgs e)
        {
            timer = new Timer();
            timer.Interval = 1000;
            timer.Tick += Timer_Tick;

            timer.Start();
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            if (ExcelApp != null)
            {
                timer.Stop();
                MessageBox.Show(ExcelApp.Version);
            }
        }



Andrei Smolin
Add-in Express Team Leader
Posted 18 Apr, 2017 10:30:09 Top
Jason Rodman




Posts: 40
Joined: 2016-11-01
So this has to be on a delay? Does this still cause excel's UI to appear and prompt to save a file?
Posted 18 Apr, 2017 10:51:44 Top
Andrei Smolin


Add-in Express team


Posts: 18844
Joined: 2006-05-11
Both variants work fine for me: no Excel UI, no prompts.


Andrei Smolin
Add-in Express Team Leader
Posted 18 Apr, 2017 11:26:43 Top
Jason Rodman




Posts: 40
Joined: 2016-11-01
This doesn't work. I set it up to fire on a timer and it crashes excel with a memory exception. I am running this in VS2017 with Excel 2013. Here is what my code looks like:


var timer = new Timer {Interval = 1000};
timer.Tick += (o, args) =>
{
	try
	{
		if (ExcelApp != null)
		{
			timer.Stop();
			// my code here
		}
	}
	catch (Exception ex)
	{
		MessageBox.Show(ex.Message);
	}
};
timer.Start();


I have this inside the OnInitialize of the XLL addin, not the COM one. I tried this both with a local timer variable, as well as with a module level variable. Also tried it with a separate event handler rather than a lambda. They all end in the same result. Here is what I could capture from the error:

System.AccessViolationException occurred
HResult=0x80004003
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source=AddinExpress.MSO.2005
Posted 20 Apr, 2017 13:19:50 Top
Andrei Smolin


Add-in Express team


Posts: 18844
Joined: 2006-05-11
Hello Jason,

Please check this project: http://temp.add-in-express.com/support/MyXLLAddin4-Initialize-ExcelApp.zip.

Make sure you use System.Windows.Forms.Timer, not any other Timer.


Andrei Smolin
Add-in Express Team Leader
Posted 21 Apr, 2017 05:41:58 Top
Jason Rodman




Posts: 40
Joined: 2016-11-01
Your example project exhibits the same behavior. Crashes excel with a memory exception. Try your project with Excel 2013 (version 15), like I am running. I noticed your project is setup to run against Excel 2010 (version 14).
Posted 21 Apr, 2017 09:06:21 Top
Andrei Smolin


Add-in Express team


Posts: 18844
Joined: 2006-05-11
Hello Jason,

My project works for me in Excel 2010-2016.

What Excel 2013 build are you using? Mine is 15.0.4919.1002 32bit.

Do you start Excel by pressing F5? If so, is Visual Studio started via the Run as administrator option?


Andrei Smolin
Add-in Express Team Leader
Posted 21 Apr, 2017 09:22:52 Top
Jason Rodman




Posts: 40
Joined: 2016-11-01
I am running Excel 2013 (15.0.4911.1000) MSO (15.0.4911.1000) 32 bit. Part of office professional plus 2013. Yes, I am running thru VS with F5 and VS is run as administrator. But, it crashes excel whether I start by debugging or running excel on its own.
Posted 21 Apr, 2017 09:30:07 Top