OpenSharedItem with Outlook interop throws a strange exception with Office 2003, works with Office 2008

I use the Interpack interaction API to open a .msg file saved from Outlook and then show a response window to allow the user to respond to it.

When you start Office 2003 OpenSharedItem (pathToMSGFile); The call throws the following exception:

Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. at Microsoft.Office.Interop.Outlook._NameSpace.OpenSharedItem(String Path) at OutlookTest.Program.Main(String[] args) 

When working with Office 2008, it works absolutely fine.

I put together a small test case, the code is as follows:

 static void Main(string[] args) { try { Application app; string pathToMSGFile = "\\\\path\\to\\foobar.msg"; if (args.Length > 0) { pathToMSGFile = args[0]; } if (!File.Exists(pathToMSGFile)) { Console.WriteLine("{0} does not exist.", pathToMSGFile); return; } Console.WriteLine("Opening {0}", pathToMSGFile); Type olType = Type.GetTypeFromProgID("Outlook.Application", false); app = Activator.CreateInstance(olType) as Application; MailItem fld = (MailItem)app.Session.OpenSharedItem(pathToMSGFile); _MailItem reply = fld.ReplyAll(); reply.Save(); reply.Display(false); Console.ReadKey(); reply.Close(OlInspectorClose.olDiscard); } catch (System.Exception ex) { Console.WriteLine(ex.ToString()); if (ex.InnerException != null) { Console.WriteLine(ex.InnerException.ToString()); } } Console.ReadKey(); } 

The application is designed as .Net 4 using the Interpack Office12 library. The same thing happens whether it is compiled for AnyCPU or x86.

+4
source share
3 answers

I note that version 11 of the interop library does not contain the OpenSharedItem() method.

It becomes available from version 12 onwards.

It seems that this operation is not available in Office 11/2003 (at least not through this method call for any version of interop lib).


I'm not sure if this is suitable for your scenario, but I had good success in the Outlook Redemption library.

From What is Outlook Redemption? :

Outlook Redemption works with the restrictions imposed by the Outlook Security Patch plus package; it provides a number of objects and functions for working with properties and functionality that are not displayed in the Outlook object model.

The Redemption library also includes a family of RDO (Redemption Data Objects) that can function as a complete replacement for the CDO 1.21 or Outlook object model.

It seems to get around some of these odd / inconsistent b / w behaviors of different versions of Outlook (either by design or as a β€œside effect” of the original goal).

If you are familiar with CDO, it will be convenient for you to work with RDO. But to be honest, I don’t know how this "maps" to Microsoft.Office.Interop.Outlook.

Contact http://www.dimastr.com/redemption/rdo/rdosession.htm

The equivalent RDO function for Session.OpenSharedItem() is equal to RDOSession.GetMessageFromMsgFile() .

NB I am in no way associated with this product, except that I used it sometimes! :-)

+4
source

This problem occurs when you try to save a message that contains a large number of attachments. When you find the message in Outlook, the menu file β†’ saveas also ends up with the same error.

0
source

Reading emails from the office of Outlook 2003 is possible, but not from the path (to the .msg file) but we can read emails in the Outlook folder (default / other folders) as a Mail-Items object.

If its encoded in the COM object library of object 12.0 outlook and used in Office 2003 (which has an outlook 11.0 object lib), then obviously this will not work!

The Error: Attempted to read or write protected memory. This is often an indication that other memory is corrupt The Error: Attempted to read or write protected memory. This is often an indication that other memory is corrupt will occur mainly if the .dll files are missing, so it is better to place the associated .dll files or install the updated office 2007,2010. it solves it.

We can read emails from a higher version of the office, such as office 2007,2010, etc. using the method as shown below.

 outlook._Application X=new Outlook.Application(); Outlook.MAPIFolder=; //...... Default Folderof outlook String Path="......\...\temp.msg"; Outlook._MailItem Mail=(Outlook.MailItem)(X.Session.OpenSharedItem(Path); 

You can access your mail and program it.

0
source

Source: https://habr.com/ru/post/1438408/


All Articles