Release Outlook Application object

G

Guest

Hi.
I'm trying to access the appointments from MS Outlook 2003 using the interop
assembly. Here is the code using .Net Framework 2.0 beta:

Microsoft.Office.Interop.Outlook.Application m_coOutlookApp =
new Microsoft.Office.Interop.Outlook.Application();
NameSpace coNameSpace = m_coOutlookApp.GetNamespace("MAPI");
coNameSpace.Logon("Outlook", null, false, true);
MAPIFolder coCalendarFolder =
coNameSpace.GetDefaultFolder(OlDefaultFolders.olFolderCalendar);
Items coItems = coCalendarFolder.Items;
foreach (object coObj in coItems)
{
AppointmentItem coAppointment = coObj as AppointmentItem;
if (null == coAppointment)
{
continue; //this isn't an appointment.
}
Console.WriteLine(coAppointment.Subject);
}
coNameSpace.Logoff();
m_coOutlookApp = null;


The code works fine, BUT:
1) I cannot find a way to release the Application object. The "Outlook.exe"
process stays loaded until my application ends (some other work is done after
checking the appointments). I already tried:
a) using "false" as value for the NewSession parameter of the Logon method;
b) calling m_coOutlookApp.Quit();
c) calling 2 times GC.Collect();
GC.WaitForPendingFinalizers();
d) calling Marshal.ReleaseComObject() until the reference count is 0;
e) using the same code without Logon(), Logoff();
So, any ideas ?
2) Sometimes if Outlook is already running when the application starts, it
stoppes responding, and cannot be closed by the user.
Sometimes if my code already run(and the "Outlook.exe" process is yet
loaded), and the user tries to start Outlook, an "Operation failed"
messagebox pops up. The user is then unable to start Outlook until my
application is closed.
How could I handle these situations?

Thanks
 

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