title: VBA Outlook code to open Mail Item and Save As text
body: <p>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).</p>
  
  <p>I seem to have two choices:</p>
  
  <p>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.</p>
  
  <p>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 <strong>File-->Open</strong> 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 <strong>File-->SaveAs</strong>.</p>
  
  <p>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.)  </p>
  
  <p>EDIT: I tried Dmitri's suggestion, and this seems to work:</p>
  
  <pre><code> 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"
  </code></pre>