In my solution I have 2 projects.
One is the controller, which in the final product is used to check if a execution is issued from console/non user input and therefo
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.
- VSTO Add-in projects. Call code in VSTO Add-ins from other Office solutions