Outlook 2007 COM add-in callback

I

Istvan

Hi,

I'm trying to migrate my COM add-in from Outlook 2003 to 2007 (both on
Windows XP), using VS 2005 Professional and VSTO 2005 SE.

The add-in was originally developed on a machine with Outlook 2003
installed, using the built in Office 2003 - Outlook Add-In template in
VS2005/VSTO.

For the migration, I recreated the entire solution on a machine with Outlook
2007, VS2005 and VSTO, using the built in Office 2007 - Outlook Add-In
template in VS2005/VSTO.

I located the program line that is causing the problem and created this very
simple test program:

-------->>>
Public Class OutlookAddIn1

Private Sub ThisAddIn_Startup( _
ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Startup

Application.COMAddIns.Item("OutlookAddIn1").Object = Me
End Sub

Private Sub ThisAddIn_Shutdown( _
ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Shutdown

End Sub

End Class
--------<<<

The line
Application.COMAddIns.Item("OutlookAddIn1").Object = Me
is based on the KB 240768 article.

When I run the Add-In, I get the following exception:

-------->>>
Specified cast is not valid.


************** Exception Text **************
System.InvalidCastException: Specified cast is not valid.
at Microsoft.Office.Core.COMAddIn.set_Object(Object RetValue)
at OutlookAddIn1.OutlookAddIn1.ThisAddIn_Startup(Object sender, EventArgs
e) in
C:\__temp\Outlook_test\OutlookAddIn1\OutlookAddIn1\OutlookAddIn1.vb:line 8
at Microsoft.Office.Tools.AddIn.OnStartup()
at OutlookAddIn1.OutlookAddIn1.FinishInitialization() in
C:\__temp\Outlook_test\OutlookAddIn1\OutlookAddIn1\OutlookAddIn1.Designer.vb:line 67
at
Microsoft.VisualStudio.Tools.Applications.Runtime.AppDomainManagerInternal.ExecutePhase(String methodName)
at
Microsoft.VisualStudio.Tools.Applications.Runtime.AppDomainManagerInternal.ExecuteEntryPointsHelper()
at
Microsoft.VisualStudio.Tools.Applications.Runtime.AppDomainManagerInternal.Microsoft.VisualStudio.Tools.Applications.Runtime.IExecuteCustomization2.ExecuteEntryPoints()


************** Loaded Assemblies **************
--------<<<

I couldn't find any information whether I'm missing something, doing
something wrong, this is not the way to implement a callback in Outlook 2007
add-ins, or whether it is a bug?

Any thoughts will be appreciated.

Thanks,

Istvan
 
K

Ken Slovak - [MVP - Outlook]

Setting that is much more complicated in VB.NET and VSTO 2005 SE. I'm
assuming you want to be able to call public methods or access public
properties in the addin from the outside world?

I use some code adapted from examples for earlier versions of VSTO written
by Outlook MVP Jay Harlow, the samples are on his www.tsbradley.net Web
site.

Here's what I use:

in a Globals module:
Public AddinObject As AutomationObject

in ThisAddIn_Startup:

AddinObject = New AutomationObject(Me)
g_ProgID = ThisAddin.ProgId

Then in ThisAddin.vb:

Public Shared ReadOnly Property ProgId() As String
Get
Dim thisAssemblyName As AssemblyName =
Assembly.GetExecutingAssembly.GetName()
Return thisAssemblyName.Name
End Get
End Property

Private myUtilities As AddinUtilities

Protected Overrides Function RequestCOMAddInAutomationServices() As
Object
If myUtilities Is Nothing Then
myUtilities = New AddinUtilities()
End If

Return myUtilities
End Function

Then after the class ends:

<ComVisibleAttribute(True), InterfaceType(ComInterfaceType.InterfaceIsDual)>
_
Public Interface IAddinUtilities
Sub CalledFromOutside()
End Interface

<ComVisibleAttribute(True), ClassInterface(ComInterfaceType.None)> _
Public Class AddinUtilities
Implements IAddinUtilities

Sub CalledFromOutside() Implements IAddinUtilities.CalledFromOutside
MessageBox.Show("This was called from outside the addin")
End Sub
End Class

Then in the AutomationObject class module:

Public Class AutomationObject
Private ReadOnly m_application As Outlook.Application

Friend Sub New(ByVal application As ThisAddin)
Try
m_application = application.Application
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class
 
I

István Becze

Thank you Ken for the code sample. As you wrote, it seems to be much more
complicated... However, I want to note again, that the one line code in my
test program (developed with VS2005 and VSTO 2005 SE for Outlook 2003) runs
fine in Outlook 2003.

My add-in implements a function that I call from a custom form command
button VBA event handler. Since I don't want to implement the entire
functionality in VBA, this was the way I called a public function in my COM
add-in code to hande the commandbutton_click event.

I'm going to implement your code, and I'll let you know about the outcome.

Thanks again,

István
 
I

István Becze

Ken,

there are three things that I don't understand.
1. In the
Protected Overrides Function RequestCOMAddInAutomationServices() As Object
statement which function in which base class is overridden?
I cannot find anything about a function RequestCOMAddInAutomationService() and
VS doesn't allow me to declare the function with "Overrides".

2. Do I still expose the public functions through the
Application.COMAddIns.Item("OutlookAddIn1").Object
property by assigning something to it?

3. If 2 is yes, do I assign:
Application.COMAddIns.Item("OutlookAddIn1").Object =
RequestCOMAddInAutomationServices()

Thank you,

István
 
K

Ken Slovak - [MVP - Outlook]

Sorry about the typo, the overridden method should be the
RequestCOMAddInAutomationService method. That declaration should be in your
Partial Public Class ThisAddIn class. The method I used for that is actually
documented by the VSTO team. See
http://msdn2.microsoft.com/en-us/li....requestcomaddinautomationservice(VS.80).aspx,
and http://blogs.msdn.com/andreww/comments/1473949.aspx for some additional
information about this.

The following lines would be all your code would need to set things up in
the addin:

AddinObject = New AutomationObject(Me)
g_ProgID = ThisAddin.ProgId

From the outside world you would locate the addin as usual in the COMAddIns
collection and use the following code, this is actually VBA code but can be
translated to whatever language you need for this, where oAddin the targeted
addin:

oAddin.Object.CalledFromOutside

Of course you can add other public methods and properties to the addin side
and call those, you aren't limited to just one. You would add a declaration
of any other public methods or properties in the Public Interface
IAddinUtilities and then provide the backing code for the method or property
in your public AddinUtilities class.
 
I

István Becze

Thank you Ken for the detailed explanation.
I could have put a little more imagination into my research for the
function...

Your comments and example were very useful. I have already implemented the
code and it is working fine.

Thank you for your help,

István
 

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