2
votes

I have managed to open a new outlook mail item from c# code successfully using the following:

using Outlook = Microsoft.Office.Interop.Outlook;


Outlook.Application oApp;
Outlook.Inspectors oInspects;
Outlook._MailItem  oMailItem;


oApp = new Outlook.Application();
oInspects = oApp.Inspectors;
oMailItem = (Microsoft.Office.Interop.Outlook._MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

oMailItem.Display(true);

my problem is as follows: when I click on the X in the corner of the mailitem window in order to close it, I receive a message asking if I want to save changes, which is OK. but after selecting the option "no", the email window closes but another one seems to appear right behind it (greyed out) and I have to click again on the X in the corner to close it.

the same happens if I click on the send button to send the email. the original window closes and then another window exactly the same (with all the content) appears but the buttons are greyed out and I have to click on the X in order to close it

I have tried to close the window using the following code (any combination of the lines below) but nothing seems to work:

oMailItem.Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olDiscard);
oMailItem.Delete();
Marshal.FinalReleaseComObject(oMailItem);
Marshal.ReleaseComObject(oApp);

if anyone has an idea on how to solve this I would greatly appreciate it. I have been busting my head for hours looking for an answer

thanks in advance

3

3 Answers

3
votes

Even though this is 4 years old, here's how i solved the problem:

while (System.Runtime.InteropServices.Marshal.ReleaseComObject(mailItem) != 0) { }
mailItem = null;
GC.Collect();
GC.WaitForPendingFinalizers();
1
votes

Try to use oMailItem.Display(false); instead 'true'

0
votes

I've tried your code with Outlook 2007 + VS 2010 Express. The problem you are facing does not occur in my environment. I tried with and without Outlook running when starting the code.

The "using" statement is part of the using section at the top of the source code, not a "using" variable reference in the routine.

The 'true' in oMailTime.Display(true) has to stay true to get a modal window.