How to call VSTO class from other c# project

风流意气都作罢 提交于 2019-12-01 00:26:28

First, to your Addin that you want to call into add an Interface:

[ComVisible(true)]
public interface IExcelUtilities
{
    bool DoSomething();
}

Next, add a class that implements the interface:

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class AddInUtilities : 
    StandardOleMarshalObject,
    IExcelUtilities
{
    public bool DoSomething()
    {
        return true;
    }
}

Then override object RequestComAddInAutomationService in ThisAddin.cs:

private AddInUtilities utilities;

protected override object RequestComAddInAutomationService()
{
   try
   {
       if (utilities == null)
       {
           utilities = new AddInUtilities();
       }

       return utilities;
    }
    catch (System.Exception ex)
    {
         // Catch your ex here
    }
}

Now you should be able to call the exposed method from your external application like this:

foreach (COMAddIn comaddin in addins)
{
     if (comaddin.ProgId.Equals("YourAddinNameHere", StringComparison.InvariantCultureIgnoreCase) == true)
     {
              bool returnvalue = comaddin.Object.DoSomething();
              break;
     }
}

for some more deep info on this subject, also read: http://blogs.msdn.com/b/andreww/archive/2008/08/11/why-your-comaddin-object-should-derive-from-standardolemarshalobject.aspx

Hope it helps :-)

This isn't an answer exactly, but for others coming across this, document-level solutions cannot expose interfaces to other solutions.

Expose an object in a VSTO Add-in to other Microsoft Office solutions.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!