【问题标题】:Extract attachments from saved .msg files using VBA使用 VBA 从保存的 .msg 文件中提取附件
【发布时间】:2017-01-16 11:50:47
【问题描述】:

我正在尝试从保存的 Outlook 邮件中提取附加的 Excel 电子表格。消息已作为 .msg 文件保存到共享文件夹中。

我正在努力让 VBA 将消息识别为文件。

我试图在下面的代码中获取消息详细信息作为概念证明。

一旦我完成了这项工作,我就可以循环文件并处理附件。

我在此站点上找到了用于从仍在 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,因为我熟悉它,但很高兴有任何替代策略建议。

【问题讨论】:

标签: excel vba outlook


【解决方案1】:

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

  • C:\temp\打开msg文件
  • 剥离所有附件到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

【讨论】:

  • 感谢您的帮助。我在 Set msg 行收到错误:运行时错误 438“对象不支持此属性或方法”
  • 修复了!在 Outlook 而不是 Excel 中运行代码,它已经工作了。非常感谢您的帮助:-)
  • 您可以从 Excel 中运行它。 (1) 参考 Outlook 对象库 (2) 将 Dim app as Outlook.Application 添加到您的声明中 (3) 使用 'app' 而不是 'Application'。
【解决方案2】:

我有一个 VBS 脚本,用于从保存在文件夹中的 msg 文件中提取所有 XLS* 附件。此脚本将附件保存在 msg 文件的同一文件夹中。我相信这可以帮助你。

Macro.vbs

'Variables
Dim ol, fso, folderPath, destPath, f, msg, i
'Loading objects
Set ol  = CreateObject("Outlook.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
'Setting MSG files path
folderPath = fso.GetParentFolderName(WScript.ScriptFullName)
'Setting destination path
destPath = folderPath   '* I am using the same 
WScript.Echo "==> "& folderPath
'Looping for files
For Each f In fso.GetFolder(folderPath).Files
    'Filtering only MSG files
    If LCase(fso.GetExtensionName(f)) = "msg" Then
        'Opening the file
        Set msg = ol.CreateItemFromTemplate(f.Path)
        'Checking if there are attachments
        If msg.Attachments.Count > 0 Then
            'Looping for attachments
            For i = 1 To msg.Attachments.Count
                'Checking if is a Excel file
                If LCase(Mid(msg.Attachments(i).FileName, InStrRev(msg.Attachments(i).FileName, ".") + 1 , 3)) = "xls" Then
                    WScript.Echo f.Name &" -> "& msg.Attachments(i).FileName
                    'Saving the attachment
                    msg.Attachments(i).SaveAsFile destPath &"\"& msg.Attachments(i).FileName
                End If
            Next
        End If
    End If
Next
MsgBox "Anexos extraidos com sucesso!"

在命令提示符下使用“cscript c:\temp\msg_files\Macro.vbs”来执行。

【讨论】:

    【解决方案3】:

    使用Namespace.OpenSharedItem。不要使用CreateItemFromTemplate - 它会清除许多属性(例如发送者和接收者相关的属性)。

    【讨论】:

    • 不知道为什么这会是一个问题,因为 OP 想要剥离附件?
    • 如果其他正在阅读此主题的人想要提取附件以外的属性,这将是一个问题。
    【解决方案4】:

    我更改了此代码,以便您可以从 Excel 而不是 Outlook 中提取附件。

    别忘了引用 Outlook 库,否则会报错

    Sub SaveOlAttachments()
    
    Dim app As Outlook.Application
    Dim msg As Outlook.MailItem
    Dim att As Outlook.Attachment
    Dim strFilePath As String
    Dim strAttPath As String
    
    Set app = New Outlook.Application
    
    'path for creating msgs
    strFilePath = "C:\Users\New folder\"
    
    'path for saving attachments
    strAttPath = "C:\Users\Extract\"
    
    strFile = Dir(strFilePath & "*.msg")
    
    Do While Len(strFile) > 0
        Set msg = app.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
    
    MsgBox "Task Completed", vbInformation
    
    End Sub
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-29
      • 1970-01-01
      • 2020-02-19
      • 1970-01-01
      • 1970-01-01
      • 2016-05-31
      • 1970-01-01
      • 2019-11-01
      相关资源
      最近更新 更多