【发布时间】:2020-08-13 22:49:31
【问题描述】:
给定一个包含数千个 Outlook MSG 文件的目录,我想使用 Excel 读取某些邮件元数据并将其映射到工作表。但是,VBA 将某些字段返回为空白,例如 Sender。
我希望每封电子邮件有一个附件。我正在尝试:
Sub SaveOlAttachments()
Dim objOL As Outlook.Application
Set objOL = New Outlook.Application
Dim fpath As String
Dim outPath As String
Dim writeRow As Long
fpath="some\path"
outPath="some\path"
writeRow = 2
strFile = Dir(fpath & "\" & "*.msg")
Do While Len(strFile) > 0
Set Msg = objOL.Session.OpenSharedItem(fpath & "\" & strFile)
If Msg.Attachments.Count > 0 Then
For Each att In Msg.Attachments
att.SaveAsFile outPath & "\" & att.Filename
Cells(writeRow, 1).Value = Msg.Subject
Cells(writeRow, 2).Value = att.Filename
Cells(writeRow, 3).Value = Msg.SentOn
Next
End If
writeRow = writeRow + 1
strFile = Dir
Loop
End Sub
但是,在我的 Locals 窗口中查看 Msg 时,我得到了 SenderEmailAddress、BCC、Body、Recipients 的空白值。
我在打开任何一封电子邮件后立即知道这是错误的。
【问题讨论】:
-
您是否尝试过使用 OutlookSpy 查看有问题的 MSG 文件(dimastr.com/outspy - 单击 OpenIMshOnOStg 按钮)?
-
writeRow仅在每个消息后递增,而不是在每个附件后递增。