简体   繁体   中英

VSTO Addin Bidirectional communication

I'm looking into the possibility of having a instance of an VSTO Word Add-in communicate with an instance of a VSTO Excel Add-in.

So far i've found an article from Microsoft on what I thought was going to be the solution: https://docs.microsoft.com/en-us/visualstudio/vsto/calling-code-in-vsto-add-ins-from-other-office-solutions?view=vs-2022&tabs=csharp

However, when one iterates over the Globals.ThisAddIn.Application.COMAddIns collection, I can only see the current Office product (Word/Excel/PowerPoint/Outlook) installed Addin's.

I have a central project that all of the add-in's share, this includes the shared interface. Each add-in implements that interface and are decorated with attributes as per the above guide.

    public interface IInterAddinCommsService
    {
        void Notify();
    }

    [ComVisible(true)]
    [ClassInterfaceAttribute(ClassInterfaceType.None)]
    public class WordAddinNotificationService : StandardOleMarshalObject, IInterAddinCommsService
    {
        public void Notify()
        {
            
        }
    }
    
    [ComVisible(true)]
    [ClassInterfaceAttribute(ClassInterfaceType.None)]
    class PowerPointAddinNotificationService : StandardOleMarshalObject, IInterAddinCommsService
    {
        public void Notify()
        {
            
        }
    }

And here is my method trying to retrieve the com objects and calling the implemenetd method

public void OnAction(Office.IRibbonControl control, bool IsPressed)
        {
            //// Notify other addin's
            var addins = Globals.ThisAddIn.Application.COMAddIns;

            foreach (Office.COMAddIn addin in addins)
            {
                var service = addin.Object as IInterAddinCommsService;
                if (service == null) continue; // skip if object doesn't implement IInterAddinCommsService
                service.Notify();
            }
        }

Any help would be greatly appreciated.

In addition to implementing the interface you need to override the RequestComAddInAutomationService method which returns an object in your add-in that can be used by other solutions. For a code example that demonstrates how to override the RequestComAddInAutomationService method, see Walkthrough: Calling Code in a VSTO Add-in from VBA . For example, that is how it should like:

private WordAddinNotificationService utilities;

protected override object RequestComAddInAutomationService()
{
    if (utilities == null)
        utilities = new WordAddinNotificationService();

    return utilities;
}

Note, the object that you return must be public, it must be visible to COM, and it must expose the IDispatch interface. If the object you return does not meet these requirements, the Visual Studio Tools for Office runtime will throw an InvalidCastException after it calls your implementation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM