OpenSharedItem - Blog - SAN Business Solutions

Search
Go to content

Main menu:

OpenSharedItem

Published by in c# · 30/1/2017 17:23:00
You try to open an email stored on the filesystem in C# programs using NameSpace.OpenSharedItem method of the Microsoft.Office.Interop.Outlook assembly. 
It is working fine for the first time the msg file is opening, but by trying to reopen the file it showing the following error:

"Cannot open file: ....msg. The file may not exist, you may not have permission to open it, or it may be open in another program.
Right-click the folder that contains the file, and then click Properties to check your permissions for the folder."

Solution
using Outlook = Microsoft.Office.Interop.Outlook;
Outlook.Application application = new Outlook.Application();
....................................................................................
Instead of:
Outlook.MailItem oItem = (Outlook.MailItem) application.Session.OpenSharedItem(string Path);
you should use:
var oItem = application.Session.OpenSharedItem(string Path) as Outlook.MailItem;
.................................................................................
oItem.Close(OlInspectorClose.olDiscard);  // now closing is possible
aapplication.Quit();
Marshal.ReleaseComObject(oItem);



Back to content | Back to main menu