2017-01-16 160 views
2

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

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

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,因为我很熟悉它,但很高兴有任何其他策略建议。任何和所有的指针感激地收到。
感谢
凯尔

+0

你看过CreateItemFromTemplate从http://stackoverflow.com/questions/7890612/vba-code-to-save-an-attachment-excel-file-from-an-outlook-email-that-was-insid/7916444#7916444? – brettdj

回答

0

VBA Code to save an attachment (excel file) from an Outlook email that was inside another email as an attachment使用CreateItemFromTemplate你可以

  • 开放味精文件从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 
+0

感谢您的帮助。我在Set msg行收到错误:运行时错误438“对象不支持此属性或方法” –

+0

已修复它!在Outlook中代码而不是Excel,它已经工作。非常感谢您的帮助:-) –

+1

您可以从Excel中运行它。 (1)引用Outlook对象库(2)将'Dim app作为Outlook.Application'添加到您的声明中(3)使用'app'而不是'Application'。 – Vlad

0

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

+0

不知道为什么这是OP想剥离附件的问题? – brettdj

+0

对于正在阅读此线程的其他人来说,如果他们想要提取除附件以外的属性,则会出现问题。 –