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
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