Share data between VSTO and Excel DNA App domains

Source: Internet
Author: User

Is there a-share data between a VSTO add in and an Excel DNA add in? Or can the Excel DNA add in is loaded into the VSTO ' s app domain?

The Excel-dna add-in is always is in a separate AppDomain. You might a try to pass an object via the AddIn's object property. Any Marshalbyref object works across AppDomains, but you need make the initial link ...

Finally I ended up with this approach:

1) Put the Sharable objects in a class lib and mark them COM visible:

namespacesharedlib{usingSystem.Runtime.InteropServices; [ComVisible (true)] [InterfaceType (cominterfacetype.interfaceisdual)] Public InterfaceIsharedobject {intGetvstoinstancenumber (); } [ComVisible (true)] [ClassInterface (ClassInterfaceType.None)] Public classSharedobject:isharedobject {Private ReadOnly intseed; Private intserial;  PublicSharedobject (intseed) {             This. Seed =seed; }         Public intGetvstoinstancenumber () {return++serial + This. Seed; }    }}

2) in VSTO create an instance of the shareable object and expose it via Requestcomaddinautomationservice:

namespacesampleexceladdin{usingSharedlib;  Public Partial classThisAddIn {Private Const intSeed =Ten; PrivateIsharedobject Sharedobject; protected Override ObjectRequestcomaddinautomationservice () {if(Sharedobject = =NULL) {Sharedobject=NewSharedobject (Seed); }            returnSharedobject; } ... code removed forbrevity ...}}

3) Now consume the shared object in the Excel DNA class Lib:

namespaceexcelfunctions{usingexceldna.integration; usingExcel =Microsoft.Office.Interop.Excel; usingSharedlib;  Public classexcelfunctions {[Excelfunction (Description="Gets A serial number from the VSTO")]         Public Static intGetvstoinstancenumber () {varApplication =(Excel.Application) exceldnautil.application; ObjectAddinname ="Sampleexceladdin"; varAddIn = Application.COMAddIns.Item (refaddinname); varUtils =(isharedobject) addin.            Object; returnUtils.        Getvstoinstancenumber (); }    }}

Share data between VSTO and Excel DNA App domains

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.