【问题标题】:Outlook messages read from a Windows folder are blank for certain values对于某些值,从 Windows 文件夹读取的 Outlook 邮件为空白
【发布时间】: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 仅在每个消息后递增,而不是在每个附件后递增。

标签: excel vba outlook


【解决方案1】:

事实证明,将文件从我的网络驱动器复制到我的 Outlook 收件箱中的文件夹中,这些值又回到了我可以在 Outlook 中再次看到它们的位置。

我没有在 Excel 中制作工作簿,而是在 Outlook 的 VBE 中使用以下方法将我需要的每封电子邮件中的数据映射到我可以保存的 csv 文件中。

Sub read_drta()

Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Set objNS = GetNamespace("MAPI")
Set objFolder = objNS.folders("my_email@address.com")
Set objFolder = objFolder.folders("Inbox")
Set objFolder = objFolder.folders("imports")
fpath = "C:\Users\Email_Tasker"

my_csv = fpath & "\data.csv"

Open my_csv For Output As #1

For Each Item In objFolder.Items

    myLine = Item.Subject & "," & Item.SentOn & "," & Item.SenderEmailAddress & "," & Item.Attachments(1).FileName
    Print #1, myLine
Next Item

Close #1
End Sub

“项目”对象包含 Outlook VBA 中的所有内容,但奇怪的是,它在 Excel 中仍然保留相同的字段为空。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-16
    • 1970-01-01
    • 1970-01-01
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多