How to call the Add-In functions from an external application?
Locked
-
Sunday, February 08, 2009 12:19 PMModeratorHow to call the Add-In functions from an external application?
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Edited by Ji.ZhouModerator Friday, February 13, 2009 10:46 AM
All Replies
-
Sunday, February 08, 2009 12:20 PMModerator
Shared Add-In uses the COMAddIn.Object manner to expose its functions to the external applications, so our implementation of OnConnection will roughly look like this:
((COMAddIn)AddInInst).Object = new MyAutomationObject();
However this method cannot be used in VSTO because setting COMAddIn.Object is only permitted during OnConnection function and the OnConnection is not visible in VSTO project.
In VSTO, to achieve this objective, we need to override the RequestCOMAddInAutomationService of the base Add-In class. In the implementation of this function, we just return a new instance of a COM Visible class.
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IAddinUtilities
{
void DoSomething();
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class AddinUtilities :
StandardOleMarshalObject,
IAddinUtilities
{
public void DoSomething()
{
Globals.ThisAddIn.CreateNewTaskPane();
}
}
public partial class ThisAddIn
{
private AddinUtilities addinUtilities;
protected override object RequestComAddInAutomationService()
{
if (addinUtilities == null)
{
addinUtilities = new AddinUtilities();
}
return addinUtilities;
}
}
Then VSTO will set the returned object to be the value of the current COMAddIn.Object for us. Later on, we can get the addInUtilities from the COMAddIn.Object and call its exposed functions with VBA or other external applications.
Private Sub CommandButton2_Click()
Dim addin As Office.COMAddIn
Dim utilities As ExcelAddinService.addinUtilities
Set addin = Application.COMAddIns("ExcelAddinService")
Set utilities = addin.Object
Call utilities.SetCellValue("a1", 456.78)
End Sub
A detailed article about this is available in Andrew’s blog here: http://blogs.msdn.com/andreww/archive/2007/01/15/vsto-Add-Ins-comaddins-and-requestcomaddinautomationservice.aspx
As to the reason why we make the AddinUtilities inherit from the StandardOleMarshalObject, please read,
(Related forum thread http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/11e4b5aa-fd42-4261-9f97-50c434a8d25e/ )
For more FAQ about Visual Studio Tools for Office, please see Visual Studio Tools for Office FAQ
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Marked As Answer by Ji.ZhouModerator Wednesday, February 11, 2009 12:42 PM
-
Wednesday, April 01, 2009 11:47 AMModerator
Add VB version codes,
<ComVisible(True)> _
<InterfaceType(ComInterfaceType.InterfaceIsDual)> _
Public Interface IAddinUtilities
Sub DoSomething()
End Interface<ComVisible(True)> _
<ClassInterface(ClassInterfaceType.None)> _
Public Class AddinUtilities
Inherits StandardOleMarshalObject
Implements IAddinUtilitiesPublic Sub DoSomething() Implements IAddinUtilities.DoSomething
Globals.ThisAddIn.CreateTaskPane()
End Sub
End ClassPartial Public Class ThisAddIn
Private addinUtilities As AddinUtilities
Protected Overrides Function RequestComAddInAutomationService() As Object
If addinUtilities Is Nothing Then
addinUtilities = New AddinUtilities()
End If
Return addinUtilities
End Function
End Class
We have published a VSTO FAQ recently, you can view them from the entry thread http://social.msdn.microsoft.com/Forums/en/vsto/thread/31b1ffbf-117b-4e8f-ad38-71614437df59. If you have any feedbacks or suggestions on this FAQ, please feel free to write us emails to colbertz@microsoft.com.

