简体   繁体   中英

VBA Outlook code to open Mail Item and Save As text

I'm receiving Outlook mails that have other Outlook mails (*.msg) as attachments. I need them in txt format (or anything else Word can open).

I seem to have two choices:

1) Save the attachments to my drive as text files, rather than msg files. I have no idea how to do that, either manually or by code.

2) Save the attachments as msg files (I got a macro here on SO that does that), then open each file and save it at txt. But File-->Open in Outlook 2010 has no option for opening msg files. The only way I can see to open the file is to (manually) view the folder in File Explorer and double-click it. Once its open, I can use File-->SaveAs .

3) I could open and save the file in VBA. Or can I? (It seems you can't record a macro in Outlook the way you can in Word or Excel, or I would have tried it.)

EDIT: I tried Dmitri's suggestion, and this seems to work:

 Dim oNamespace As NameSpace
 Dim oFolder As Folder


 ' Get a reference to a NameSpace object.
   Set oNamespace = Application.GetNamespace("MAPI")

 ' Open the file containing the shared item.
   Set oSharedItem = oNamespace.OpenSharedItem("D:\temp.msg")

' Save the item to the  folder.
   oSharedItem.SaveAs "D:\temp.txt"

Save the embedded message attachments as MSG files ( Attachment.SaveAsFile ), then open then using Namespace.OpenSharedItem .

If you want to access the embedded message attachments as messages without saving them, you'd need either Extended MAPI ( IAttach::OpenProperty(PR_ATTACH_DATA_OBJ, IID_IMessage, ...) , C++ or Delphi only) or Redemption (it exposes Attachment.EmbeddedMsg property).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM