Re: Calling a VSTO AddIn from C#

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Hi Ken!

I followed the steps you laid out here, and it works - but, alas, just
to that point that when I query the .Object property of the AddIn. I
get back a System.__COMObject, which cannot be cast to my interface.

Any idea about this?

regards
M


Ken Slovak - [MVP - Outlook] wrote:
For Outlook code would look something like this. I'm taking this from a VSTO
2005 SE project, but it should be similar in VSTO 2005.

In the ThisApplication class:

private AddinUtilities addinUtilities;

protected override object RequestComAddInAutomationService()
{
if (addinUtilities == null)
{
addinUtilities = new AddinUtilities();
}
return addinUtilities;
// return base.RequestComAddInAutomationService()
}

// end of ThisApplication class

[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IAddinUtilities
{
void CalledFromOutside();
}

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class AddinUtilities : IAddinUtilities
{
public void CalledFromOutside()
{
MessageBox.Show("This was called from outside the addin");
}
}

In external code, for example calling this from VBA, the call would look
like this:

Sub TestCallFromOutside()
Dim oAddin As Office.COMAddIn

Set oAddin = Application.COMAddIns.Item("MyAddin")
oAddin.Object.CalledFromOutside
End Sub

Of course in this context Application would only be valid when run in the
Outlook VBA project. For a different context you'd need to get a handle to
the Outlook.Application object and use that.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Absolute Beginner's Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


"AjayB" <AjayB@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:E04B6153-60D0-4E69-954B-D77A2AA32F12@xxxxxxxxxxxxxxxx
Hi Dennis,

I am using VSTO 2005,
I have created a VSTO Addin "OutlookAddin1" it has a public method in
class
"ThisApplication" i want to call it from another external appication but i
am not able to do it. How can i call the method from external application
i tried below code but it does not gives the object so not able to call
the
method

OutlookAddin1.ThisApplication testMailApp;
object testapp = new object();
testapp = "OutlookAddin1";

Outlook.Application olApp = new Outlook.ApplicationClass();

Microsoft.Office.Core.COMAddIn comAddIn;

Microsoft.Office.Core.COMAddIns addins;

addins = olApp.COMAddIns;

comAddIn = addins.Item(ref testapp);

testMailApp = comAddIn.Object as OutlookAddin1.ThisApplication;

testMailApp.CreateMessage("test", "test", "test"); //This is
the
method i want to call but since from above line testMailApp we dont get
any
instance we cant call the method.

Please let me know any solution for it

Thanks & Regards
Ajay Bhalekar

.


Quantcast