尝试使用VBA从保存的.msg文件中提取Outlook附件

我试图从保存的Outlook消息中提取附件,以便从附加的Excel电子表格中挖掘数据。 消息已经被保存到.msg文件的共享文件夹中,我正努力让VBA甚至将这些消息识别为文件。 我最初试图在下面的代码中获取消息细节作为概念certificate。

一旦我有这个工作,我可以通过循环处理文件和处理附件。 我在这个网站上发现了许多代码,用于从仍然在Outlook中的电子邮件中提取附件,但是我没有访问Outlook文件夹的function,原始邮件也被删除了。

Sub ExtractExcel() Dim aExcel As Outlook.Attachment Dim stFilePath As String Dim stFileName As String Dim stAttName As String Dim stSaveFolder As String Dim oEmail As Outlook.MailItem '~~> Outlook Variables for email Dim eSender As String, dtRecvd As String, dtSent As String Dim sSubj As String, sMsg As String stFilePath = "Y:\Purchasing\The Team\User Name\Supply Chain Admin - Outlook\New-Revised Orders\FW Mail Order Daffodil.msg" stSaveFolder = "C:\Projects\SOTD\PO_Excel" Debug.Print stFilePath Debug.Print stSaveFolder oEmail = stFilePath With oEmail eSender = oEmail.SenderEmailAddress dtRecvd = oEmail.ReceivedTime dtSent = oEmail.CreationTime sSubj = oEmail.Subject sMsg = oEmail.Body Debug.Print eSender Debug.Print dtRecvd Debug.Print dtSent Debug.Print sSubj Debug.Print sMsg End With End Sub 

我正在使用Excel VBA,因为我很熟悉它,但很高兴有任何替代策略build议。 任何和所有的指针感激地收到。
谢谢
凯尔

使用VBA代码中的 CreateItemFromTemplate 将附件(excel文件)保存在另一封电子邮件中的Outlook电子邮件中作为附件

  • 打开msg文件从C:\temp\
  • 将所有附件剥离到C:\temp1\

 Sub SaveOlAttachments() Dim msg As Outlook.MailItem Dim att As Outlook.Attachment Dim strFilePath As String Dim strAttPath As String 'path for creating msgs strFilePath = "C:\temp\" 'path for saving attachments strAttPath = "C:\temp1\" strFile = Dir(strFilePath & "*.msg") Do While Len(strFile) > 0 Set msg = Application.CreateItemFromTemplate(strFilePath & strFile) If msg.Attachments.Count > 0 Then For Each att In msg.Attachments att.SaveAsFile strAttPath & att.FileName Next End If strFile = Dir Loop End Sub 

使用Namespace.OpenSharedItem 。 不要使用CreateItemFromTemplate – 它会清除很多属性(如发件人相关的属性)。