Exception during Outlook Interop

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
 
C

createwindow

Dear John,

Have you tried putting Marshal.ReleaseComObject(em) after
ns.Logoff() ??

Hope that helps.
CreateWindow
http://mymessagetaker.com
The while-you-were-out message program you have been looking for!
 
J

John

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
 
M

Mike Williams

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

Mike Williams

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:
 

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