Register AddIn pragramatically

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

Register AddIn pragramatically
Register XLL AddIn from COM Add or Regsiter COM AddIn from XLL Addin 
Dirk




Posts: 73
Joined: 2017-10-05
Hello
We have an AddIn that contains a COM and a XLL AddIn in one .NET Assembly. If one is registered and running in excel but the other is not, the one that is running should register the other programmatically.
We want to prevent the user from using the AddIn manager from excel and doing that manually.

How can we achieve this?

I tried this ExcelApplication.AddIns.Add("<path to addin>") and after that set the Installed property to true.

But I'm not sure if this is your recommended way doing this. Is there an other option, your recommend?

Best Regards
Dirk
Posted 20 Mar, 2019 12:44:15 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Hello Dirk,

Turning the add-in on/off programmatically may not be user-friendly. The user may turn your add-in off for a number of reasons: to prevent performance issues, to get more free UI area, to turn the add-in off because it constantly crashes, etc.

I suggest that you inform the user that the other add-in is required for your add-in to function. Still, it may not be possible to do this from the XLL add-in as we do not suggest using the Excel object model in an XLL add-in.


Andrei Smolin
Add-in Express Team Leader
Posted 21 Mar, 2019 04:34:04 Top
Dirk




Posts: 73
Joined: 2017-10-05
Ok. The point is that we need two addins: 1 x COM and 1 x XLL . If the XLL is missing there is not customer intention to not activate it and is expectation that the AddIn as a whole works.
And yes. It is not recommended to use Excel object from XLL or even not possible. What I want to know, is there a safe way to activate a COM AddIn from inside a XLL AddIn. Which are the restrictions. ADXXLLModule has the property HostApplication. I assume this is the excel object. The question is when are we allowed to use the Excel Application object from the ADXXLLModule and how can we safe install the COM Addin. May be there are some events, which can be used for this purpose.
Posted 21 Mar, 2019 08:18:32 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Dirk Reu? writes:
What I want to know, is there a safe way to activate a COM AddIn from inside a XLL AddIn.


The point is: if the COM add-in is turned off *not* by the user, you won't be able to turn it on. This will be the case e.g. if your add-in crashes the host application and it disables the add-in. There are quite many such cases; check section Troubleshooting add-in loading, see the PDF file in the folder {Add-in Express}\Docs on your development PC.

Also, you would need to handle the scenario in which the COM add-in is missing e.g. because of a registration-time issue.

We've never tied to turn on a COM add-in from an XLL add-in. I don't think there's a guaranteed way to do so. You should understand that Excel may not expect a call into the Excel object model from your XLL add-in.

Consider this approach. When your XLL function is called, you call ADXXLLModule.SendMessage() to post a custom windows message (which is an integer > 1024) to a hidden window that Add-in Express creates for your XLL add-in (like it does the same for every COM add-in). When the hidden window receives the message, you'll get the OnSendMessage event on the XLL module. By that moment, your XLL call will end and, I suppose, your event handler will be run in the context that allows turning the COM add-in on. We recommend not to perform COM calls (e.g. turn on your COM add-in) right in the context of the XLL call.

There's an additional difficulty though. To turn the COM add-in on, you need to have an Excel.Application object and the only way to retrieve it is to call the ExcelApp property. But it isn't guaranteed that you get a non-null value on the first call. And we don't recommend calling that property right at startup.

In the end, you check if your add-in exists in the ExcelApp.COMAddins collection and try to turn it on via Office.COMAddin.Connect = true. This may work. Still, I wouldn't be surprised if it ends with a crash.


Andrei Smolin
Add-in Express Team Leader
Posted 21 Mar, 2019 09:54:17 Top
Dirk




Posts: 73
Joined: 2017-10-05
Ok thanks for the idea of sending a message. We will try this.
Posted 22 Mar, 2019 03:23:51 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
You are welcome.


Andrei Smolin
Add-in Express Team Leader
Posted 22 Mar, 2019 05:43:24 Top