Reference to AddIn

N

Nenad

I am programming Outlook 2003 AddIn using Visual Studio 2008 and C#.

I need to pass a reference to AddIn to user control embedded in Folder Home
Page, but it is always a null. I already found out that it's a security
barrier which prevents passing a reference.

Did anyone knows any other way to pass a reference to C# AddIn?

Thanks, Nenad
 
K

Ken Slovak - [MVP - Outlook]

You need to make the addin COM visible and then depending on whether or not
it's a VSTO addin make public a reference to the addin if you want to call
on methods or properties of the addin.

I don't have anything for VS 2008, but I do have C# templates for VS 2005
for both shared addins and VSTO 2005SE addins that shows how to expose your
addin and methods/properties in it to outside code. You can find them at
http://www.slovaktech.com/outlook_2007_templates.htm. The templates are all
Outlook 2007 specific.

To just reference the addin as an Office.COMAddIn you would use code
something like this assuming olApp is your Outlook.Application object
reference:

Office.COMAddIn addin = olApp.COMAddIns.Item("MyAddinName");

If your addin is shared and uses a Connect class that would look like this:
"MyAddinName.Connect".
 
N

Nenad

I can get a reference to Office.COMAddIn, but what I really need is a
reference to TheAddIn (which is a subclass of a Microsoft.Office.Tools.AddIn
class and is inherited from
Microsoft.VisualStudio.Tools.Applications.Runtime.IStartup interface).

This is code from Initialize(Outlook.Application app) method from user
control hosted in FHP:

object name = "MyAddIn";
Office.COMAddIn addin = (Office.COMAddIn)app.COMAddIns.Item(ref
name);
ThisAddIn add = addin.Object as AddIn.ThisAddIn;

I was hoping that I will get a reference to VSTO AddIn using addin.Object
property, but it is always null. I also tried to set that property in the
start-up event of the add-in, but it caused TypeMismatch error, so I assume
that it is a misleading.

Anyway, I have set [ComVisible(true)] attribute on the add-in, but it didn't
helped.

Thanks for templates, they are great, but I didn't find what I needed there.

Thanks
 
K

Ken Slovak - [MVP - Outlook]

You didn't look closely enough at the templates. Look at the code at the end
of the ThisAddin class in the section starting with this line:

public static AutomationObject AddinObject = null

That code and what follows in the override and interfaces, along with the
AutomationObject class are what you need. That will expose the addin and
from there the code in the CalledFromOutside method shows how to expose
properties to the outside. You can modify that to call methods in your addin
code also.

To call from outside you'd use something like this (VBA code):

Dim oAddin As Office.COMAddIn

Set oAddin = Application.COMAddIns.Item("MyVSTOAddIn") ' the addin name
here
If Not (oAddin Is Nothing)
oAddin.Object.CalledFromOutside
End If
 
N

Nenad

Thanks, that is exactly what I have asked for.

One final question: As I can see, using this technique, only static methods
and properties can be invoked on add-in class?

Ken Slovak - said:
You didn't look closely enough at the templates. Look at the code at the end
of the ThisAddin class in the section starting with this line:

public static AutomationObject AddinObject = null

That code and what follows in the override and interfaces, along with the
AutomationObject class are what you need. That will expose the addin and
from there the code in the CalledFromOutside method shows how to expose
properties to the outside. You can modify that to call methods in your addin
code also.

To call from outside you'd use something like this (VBA code):

Dim oAddin As Office.COMAddIn

Set oAddin = Application.COMAddIns.Item("MyVSTOAddIn") ' the addin name
here
If Not (oAddin Is Nothing)
oAddin.Object.CalledFromOutside
End If




Nenad said:
I can get a reference to Office.COMAddIn, but what I really need is a
reference to TheAddIn (which is a subclass of a
Microsoft.Office.Tools.AddIn
class and is inherited from
Microsoft.VisualStudio.Tools.Applications.Runtime.IStartup interface).

This is code from Initialize(Outlook.Application app) method from user
control hosted in FHP:

object name = "MyAddIn";
Office.COMAddIn addin = (Office.COMAddIn)app.COMAddIns.Item(ref
name);
ThisAddIn add = addin.Object as AddIn.ThisAddIn;

I was hoping that I will get a reference to VSTO AddIn using addin.Object
property, but it is always null. I also tried to set that property in the
start-up event of the add-in, but it caused TypeMismatch error, so I
assume
that it is a misleading.

Anyway, I have set [ComVisible(true)] attribute on the add-in, but it
didn't
helped.

Thanks for templates, they are great, but I didn't find what I needed
there.

Thanks
 
K

Ken Slovak - [MVP - Outlook]

I believe that static is a requirement, but I'm not positive. Try it without
and see if it works. If it doesn't that still lets you set the externally
visible interfaces as static and still work with non-static methods and
properties.
 
N

Nenad

No, it is not necessary to be static, not even a add-in member
field/property. The only requirement is that an instance of a class that is
returned from a RequestComAddInAutomationService event handler is
implementing a given COM interface.
I am using code like this:
protected override object RequestComAddInAutomationService()
{
PreInitialize();
return new PreInitializeData(factory, client);
//return base.RequestComAddInAutomationService();
}
 
Y

YM

Hi Ken,

I'm trying to access functions in add-in created by VB6.
I noticed you have vb6 version of template as well. This template include a
function 'CalledFromOutside'. To make the function visible, is it enough to
make the method public? I do not see any of the automation code in vb6
template.

Thanks

Y

Ken Slovak - said:
You didn't look closely enough at the templates. Look at the code at the end
of the ThisAddin class in the section starting with this line:

public static AutomationObject AddinObject = null

That code and what follows in the override and interfaces, along with the
AutomationObject class are what you need. That will expose the addin and
from there the code in the CalledFromOutside method shows how to expose
properties to the outside. You can modify that to call methods in your addin
code also.

To call from outside you'd use something like this (VBA code):

Dim oAddin As Office.COMAddIn

Set oAddin = Application.COMAddIns.Item("MyVSTOAddIn") ' the addin name
here
If Not (oAddin Is Nothing)
oAddin.Object.CalledFromOutside
End If




Nenad said:
I can get a reference to Office.COMAddIn, but what I really need is a
reference to TheAddIn (which is a subclass of a
Microsoft.Office.Tools.AddIn
class and is inherited from
Microsoft.VisualStudio.Tools.Applications.Runtime.IStartup interface).

This is code from Initialize(Outlook.Application app) method from user
control hosted in FHP:

object name = "MyAddIn";
Office.COMAddIn addin = (Office.COMAddIn)app.COMAddIns.Item(ref
name);
ThisAddIn add = addin.Object as AddIn.ThisAddIn;

I was hoping that I will get a reference to VSTO AddIn using addin.Object
property, but it is always null. I also tried to set that property in the
start-up event of the add-in, but it caused TypeMismatch error, so I
assume
that it is a misleading.

Anyway, I have set [ComVisible(true)] attribute on the add-in, but it
didn't
helped.

Thanks for templates, they are great, but I didn't find what I needed
there.

Thanks
 
K

Ken Slovak - [MVP - Outlook]

Using the template as is will expose that method to the outside.

How you do these things varies from VB6 to managed code. You don't have the
COM Interop getting in the way with VB6 code.
 
Y

YM

Hi Ken,

What I'm trying to do is accessing some public functions in com add-in
created with vb6 from my add-in created using vs2005. Both of them are
shared add-ins.

My code looks like following:
Dim oAddin As Microsoft.Office.Core.COMAddIn
Dim addins As Microsoft.Office.Core.COMAddIns
addins = m_olApp.COMAddIns
oAddin = addins.Item("Test.Connect")

Or
oAddin = m_olApp.COMAddIns.Item("Test.Connect")

Above works, but when I try to access oAddin.Object, it returns 'nothing'.

I have added public function in vb6 addin like following:
Public Sub CalledFromOutside()
MsgBox "This is called from outside"
End Sub

Am I missing something?

Thanks in advance.

Y
 
K

Ken Slovak - [MVP - Outlook]

What is Test.Connect, is that the VB6 addin?

In the VB6 addin do you have something like this in your startup code in
OnConnection:

AddInInst.Object = Me

Your CalledFromOutside() procedure in the VB6 code would also be declared as
Public.

Then in managed code I'd call the unmanaged code using this:

oAddin.Object.CalledFromOutside()

See if that works for you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top