0

So I have the following code, which extracts all attachments from a Contact item (residing in a shared folder):

            Outlook._Application objOutlook; //declare Outlook application
            objOutlook = new Outlook.Application(); //create it
            Outlook._NameSpace objNS = objOutlook.Session; //create new session
            Outlook.MAPIFolder oAllPublicFolders; //what it says on the tin
            Outlook.MAPIFolder oPublicFolders; // as above
            Outlook.MAPIFolder objContacts; //as above
            Outlook.Items itmsFiltered; //the filtered items list
            oPublicFolders = objNS.Folders["Public Folders"];
            oAllPublicFolders = oPublicFolders.Folders["All Public Folders"];
            objContacts = oAllPublicFolders.Folders["Global Contacts"];

            itmsFiltered = objContacts.Items.Restrict(strFilter);//restrict the search to our filter terms

            for (int i = 1; i <= itmsFiltered.Count; i++) //loop through filtered items
            {
                var item = itmsFiltered[i];

                Contact ctctNew = new Contact(); //create new contact

                foreach (Outlook.Attachment oa in item.Attachments)
                { ctctNew.ImportedAttachments.Add(oa); }

                lstContacts.Add(ctctNew); // add to the list that will be displayed in the OLV
            }

            return lstContacts;

This seems to work fine.

I then try and save these to a file thus:

            if (!System.IO.Directory.Exists(strPath))
            { System.IO.Directory.CreateDirectory(strPath); }

            foreach (Outlook.Attachment o in ctLoaded.ImportedAttachments)
            {
                string strFilePath = strPath + @"\" + o.FileName;
                if (!File.Exists(strFilePath))
                {
                    o.SaveAsFile(strPath + @"\" + o.FileName); //exception here
                }
            }

This last bit works fine for filetypes that are NOT .msg - i.e. it works fine for .pdf files etc; if it's an .msg file then I get the following exception:

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in Event Management System.exe

Additional information: Cannot save the attachment. Could not complete the operation. One or more parameter values are not valid.

Can anyone shed any light?

Thanks

UPDATE: I've discovered that it's only some of the .msg files that fail to save; some of them work fine. It seems that files attached by certain users seem to work, and files by other users don't; I'm guessing it might be to do with how they attach them?

Also it appears that the code DOES save the file (despite throwing the exception), but it appears to be corrupted - a 3Kb .msg file appears in the relevant folder. Outlook won't open it, I just get a "Unable to read the item." messagebox from Outlook if I try to open the file.

Jez Clark
  • 383
  • 5
  • 19

1 Answers1

0

Just as a guess, what is the value of o.FileName for a msg attachment? It is possibly not a valid file name.

simon at rcl
  • 7,326
  • 1
  • 17
  • 24
  • It's valid. E.g. X:\quotation.msg caused an exception, but X:\quotation.pdf didn't. – Jez Clark Oct 07 '14 at 16:15
  • Any error numbers with the exception? Or an InnerException? – simon at rcl Oct 07 '14 at 16:17
  • Nope, nothing; just the message I posted. – Jez Clark Oct 07 '14 at 16:21
  • That's odd. Every COMException I've seen with Outlook has a COM error number (or HRESULT) attached somewhere. Without one, you're pretty stuck I'm afraid. – simon at rcl Oct 07 '14 at 16:23
  • Unless there's some setting in VS2013 I need to change to be able to see said error number (I can't see anything that looks like it would help), there isn't one. – Jez Clark Oct 07 '14 at 16:25
  • Nope. It would be a property of the Exception object. – simon at rcl Oct 07 '14 at 16:31
  • Apparently VS2013 Express hides exception details from you on debug. Found out the `HResult` by looking in the Locals window, it's `-1320746921`; `InnerException` is null. – Jez Clark Oct 07 '14 at 16:42
  • OK. That's a really odd HResult - I don't think it is one, in fact, at least not a COM one (wrong range: in hex that number is 0xB1470057, and all COM hresults start 0x8......). Are there any other numbers you can see? What happens if you right click on the exception object in code: does it give you a 'QuickWatch' option? – simon at rcl Oct 07 '14 at 16:49
  • That gives me the same stuff as looking in the Locals window. However, I did get a different HResult this time around: `-2117664681` – Jez Clark Oct 07 '14 at 17:00
  • Poking around in the heirarchy of the attachment itself I did find this (no idea if it is relevant): `ExchangeConnectionMode = {System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException: Member not found. (Exception from HRESULT: 0x80020003 (DISP_E_MEMBERNOTFOUND)) --- End of inner exc...` – Jez Clark Oct 07 '14 at 17:06
  • Ah ha! That looks a lot more likely! Most HRESULTs will be 0x800xxxxx. This says that it's using the IDespatch interface of an object but that the method you're calling on the object is not present on it. There are lots of hits for this. You will have to have a browse around. It is possible that o.SaveAsFile is not valid, and you will have to cast it to (from memory) an IAttachment or _IAttachment type. I think that's area to look for anyway. – simon at rcl Oct 07 '14 at 17:16
  • The only examples I could see of using SaveAsFile were is VBA, not C# or VB.NET. It is quite possible that the Primary Interop Assemblies do not expose that method; I last used them in Outlook 2007 and they were not easy. Can you look at the Object Browser (under the View menu) in VS Express? you can spelunk your way around the exposed interfaces and types and their methods and properties. I remember I had to do a fair amount of that... – simon at rcl Oct 07 '14 at 17:22
  • I've had a good look around and can't see anything of any use in Object Browser in VS. I can't see an `IAttachment` or `_IAttachment` interface anywhere either, just `Attachment` (which `o` already is) – Jez Clark Oct 08 '14 at 09:51
  • I've dug out some old code of mine; sadly I never used the Attachment type. However, your error is 0x80020003 which is an advance, I suppose. Sorry.... – simon at rcl Oct 08 '14 at 15:40