Creating email in outlook

J

John

Hi

I am trying to find the way to create an email in outlook. I am using the
code given at the end. It works fine the first time but calling the code
second time gives the following error;

Unhandled Exception: System.Reflection.TargetInvocationException: Exception
has been thrown by the target of an invocation. --->
System.Runtime.InteropServices.InvalidComObjectException: COM object that
has been separated from its underlying RCW can not be used.
at Outlook.ApplicationEvents_EventProvider..ctor(Object )

The error comes on the 'OutlookApp = New Outlook.Application' line. Any
ideas what I am missing?

Thanks

Regards

= Code Follows =======================

Imports System.Runtime.InteropServices

Public Class clsOfficeWrapper
Private WithEvents OutlookApp As Outlook.Application
Private WithEvents em As Outlook.MailItem

Public Sub NewEmail(ByVal EmailTemplate As String, ByVal SendTo As String)
Dim ns As Outlook.NameSpace

OutlookApp = New Outlook.Application ' <=== This lines give the
exception second time round
ns = OutlookApp.GetNamespace("MAPI")
ns.Logon()

em = DirectCast(OutlookApp.CreateItemFromTemplate(EmailTemplate),
Outlook.MailItem)
.To = SendTo
.Display(False)

Marshal.ReleaseComObject(em)
ns.Logoff()
Marshal.ReleaseComObject(ns)
Marshal.ReleaseComObject(OutlookApp)
End Sub
....

End Class
 
J

Jay B. Harlow [MVP - Outlook]

John,
Marshal.ReleaseComObject(OutlookApp)

Do not use ReleaseComObject if you are using WithEvents! ReleaseComObject
will release the com object, which means the com object itself is no longer
valid. WithEvents will try to remove the event handlers on the previous
object (the one you just destroyed) when you create a new instance of it.
The previous object is a .NET object, that had its com object destroyed,
WithEvents is not able to remove the COM event handlers, hence the
exception!
Public Class clsOfficeWrapper
Private WithEvents OutlookApp As Outlook.Application
Private WithEvents em As Outlook.MailItem

Seeing as OutlookApp & em are class level variables you should create them
in the class's constructor (Sub New) and destroy them (ReleaseComObject)
using the Disposable/Finalization pattern.

For info on implementing Dispose along with Finalize (you really should
implement both!) see:

http://msdn.microsoft.com/library/d.../en-us/cpgenref/html/cpconFinalizeDispose.asp

If you truly need to handle the events in the NewEmail method I would
suggest you use AddHandler & RemoveHandler and move the variables local to
the Subroutine.
Public Sub NewEmail(ByVal EmailTemplate As String, ByVal SendTo As String)
Dim OutlookApp As Outlook.Application
AddHandler OutlookApp...
Dim em As Outlook.MailItem
AddHandler em...
Dim ns As Outlook.NameSpace

OutlookApp = New Outlook.Application ' <=== This lines give the
exception second time round
ns = OutlookApp.GetNamespace("MAPI")
ns.Logon()

em = DirectCast(OutlookApp.CreateItemFromTemplate(EmailTemplate),
Outlook.MailItem)
.To = SendTo
.Display(False)
RemoveHandler em...
Marshal.ReleaseComObject(em)
ns.Logoff()
Marshal.ReleaseComObject(ns) RemoveHandler OutlookApp...
Marshal.ReleaseComObject(OutlookApp)
End Sub

Of course if you are not handling any events then the AddHandler &
RemoveHandler are not needed...

Hope this helps
Jay
 
J

John

Thanks. Great. That has worked nicely.

Regards


Jay B. Harlow said:
John,

Do not use ReleaseComObject if you are using WithEvents! ReleaseComObject
will release the com object, which means the com object itself is no longer
valid. WithEvents will try to remove the event handlers on the previous
object (the one you just destroyed) when you create a new instance of it.
The previous object is a .NET object, that had its com object destroyed,
WithEvents is not able to remove the COM event handlers, hence the
exception!


Seeing as OutlookApp & em are class level variables you should create them
in the class's constructor (Sub New) and destroy them (ReleaseComObject)
using the Disposable/Finalization pattern.

For info on implementing Dispose along with Finalize (you really should
implement both!) see:

http://msdn.microsoft.com/library/d.../en-us/cpgenref/html/cpconFinalizeDispose.asp

If you truly need to handle the events in the NewEmail method I would
suggest you use AddHandler & RemoveHandler and move the variables local to
the Subroutine.
String)
Dim OutlookApp As Outlook.Application
AddHandler OutlookApp...
Dim em As Outlook.MailItem
AddHandler em...

Of course if you are not handling any events then the AddHandler &
RemoveHandler are not needed...

Hope this helps
Jay
 

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