Exception during Outlook Interop

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

I am using below code to send email from a vb.net app;

Public WithEvents OutlookApp As Outlook.Application
Dim ns As Outlook.NameSpace

OutlookApp = New Outlook.Application
ns = OutlookApp.GetNamespace("MAPI")
ns.Logon()

Dim em As Outlook.MailItem
em = CType(OutlookApp.CreateItem(Outlook.OlItemType.olMailItem),
Outlook.MailItem)
em.Display(False)
Marshal.ReleaseComObject(em)

ns.Logoff()
Marshal.ReleaseComObject(ns)
Marshal.ReleaseComObject(OutlookApp)

The code works fine for the first time but running it again produces the
following error;

System.Reflection.TargetInvocationException was unhandled by user code
Message="Exception has been thrown by the target of an invocation."
Source="mscorlib"

InnerException: System.Runtime.InteropServices.InvalidComObjectException
Message="COM object that has been separated from its underlying RCW
cannot be used."
Source="Interop.Outlook"
StackTrace:
at Outlook.ApplicationEvents_EventProvider..ctor(Object )

What is the problem and how can I fix it?

Thanks

Regards
 
Hi

Tried

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

and

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

Still no luck.

Thanks

Regards
 
I am using below code to send email from a vb.net app;

I don't know whether anyone from Microsoft regularly reads the Visual Basic
groups but if they do then I am very surprised that they permit one of their
own MVPs to engage in such outrageous long term trolling activities in one
of their own public newsgroups, such as the activity that the person who
purports to be Bill McCarthy has engaged in on the
microsoft.public.vb.general.discussion group for many months. If this man
belongs to you:

https://mvp.support.microsoft.com/profile=B2D0BB02-3E35-4293-B4B9-25680609CCB8

.. . . then perhaps you might like to look at his activity in that group.
Here for example is one of his very latest offerings:
 
Hi
Tried
ns.Logoff()

I don't know whether anyone from Microsoft regularly reads the Visual Basic
groups but if they do then I am very surprised that they permit one of their
own MVPs to engage in such outrageous long term trolling activities in one
of their own public newsgroups, such as the activity that the person who
purports to be Bill McCarthy has engaged in on the
microsoft.public.vb.general.discussion group for many months. If this man
belongs to you:

https://mvp.support.microsoft.com/profile=B2D0BB02-3E35-4293-B4B9-25680609CCB8

.. . . then perhaps you might like to look at his activity in that group.
Here for example is one of his very latest offerings:
 
Back
Top