Shared object cannot be accessed in ExcelFunction

24 views
Skip to first unread message

Johnny24

unread,
Sep 13, 2022, 11:39:25 AM9/13/22
to Excel-DNA
Hello,
maybe it's my fault, but I cannot understand the logic behing this.
I built an empty project with Excel-DNA, and I have a shared (static) object in a class named ThisAddIn.

Public Shared MyObj as Object

    Private Sub ThisAddIn_Startup() Handles Me.Startup
        MyObj = new MyObjClass() ' from external library
    End Sub

Everything works fine, except accessing the object from Excel function. For instance:

    <ExcelFunction(Name:="MyStrFunc", Description:="Return a property of MyObj")>
    Public Shared Function  MyStrFunc() As String
        Return MyObj.myProperty' here MyObj is nothing
    End Function

I also tried:

    <ExcelFunction(Name:="MyStrFunc", Description:="Return a property of MyObj")>
    Public Shared Function  MyStrFunc() As String
        Return Globals.ThisAddIn.MyObj.myProperty ' again, MyObj  is nothing
    End Function


What am I doing wrong? I'd expected that MyObj would be available even in  ExcelFunctions. In other parts of the code it's all fine, only ExcelFunctions are not working.

thanks in advance,
Giovanni

Govert van Drimmelen

unread,
Sep 13, 2022, 12:08:15 PM9/13/22
to exce...@googlegroups.com

Hi Giovanni,

 

You seem to be mixing a VSTO add-in (ThisAddIn etc.) and an Excel-DNA add-in into the same library.

This does not work well.

 

What is possibly happening in this case is that your code is loaded as a VSTO add-in into one AppDomain, and running code like ThisAddIn_Startup.

Then the code is loaded a second time as an Excel-DNA add-in, into a different AppDomain, and your functions registered and run in this AppDomain.

Shared objects are not ‘shared’ between AppDomains.

 

My suggestion is to neve mix VSTO (anything Microsoft.Office.Tools.*) and Excel-DNA in this same project.

 

Nearly all functionality from VSTO is available from the Excel-DNA add-in, though sometimes you have to do a little bit more work.

Is there a specific reason you are making this a VSTO add-in?

 

-Govert

--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/exceldna/8e19272b-05ba-4514-9353-93035bd68651n%40googlegroups.com.

Johnny24

unread,
Sep 13, 2022, 1:57:17 PM9/13/22
to Excel-DNA
Yes, I mixed VSTO and Excel-DNA just to have visual design of ribbon bar. I suspected this would be the problem, but not sure before your reply (I was thinking  the AppDomain was unique). I'll have a closer look to the ribbon management in Excel-DNA, initially the XML for the ribbon discouraged me.
thanks,
Giovanni
Reply all
Reply to author
Forward
0 new messages