提取电子邮件数据

Extracting Email Data

提问人:Amir Shahzad 提问时间:11/14/2023 最后编辑:CommunityAmir Shahzad 更新时间:11/21/2023 访问量:28

问:

我正在尝试将数据从Outlook邮件提取到Excel工作表。

Sub ExtractOutlookEmails()
        
    Dim outlookApp As Object
    Dim outlookNamespace As Object
    Dim outlookFolder As Object
    Dim outlookItem As Object
        
    Dim ws As Worksheet
    Dim rowNumber As Long
    Set ws = ThisWorkbook.Sheets("Sheet1") 
    
    Set outlookApp = CreateObject("Outlook.Application")
    Set outlookNamespace = outlookApp.GetNamespace("MAPI")
    
    Set outlookFolder = outlookNamespace.GetNamespace("MAPI").GetDefaultFolder(6) ' 6 represents the Inbox folder
    
    For Each outlookItem In outlookFolder.Items
        If TypeOf outlookItem Is Object  And outlookItem.Class = 43 Then ' 43 represents Mail Item
            rowNumber = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row + 1
            ws.Cells(rowNumber, 1).Value = outlookItem.Subject
            ws.Cells(rowNumber, 2).Value = outlookItem.SenderEmailAddress
            ws.Cells(rowNumber, 3).Value = outlookItem.ReceivedTime
        End If
    Next outlookItem
    
    Set outlookItem = Nothing
    Set outlookFolder = Nothing
    Set outlookNamespace = Nothing
    Set outlookApp = Nothing
End Sub

这给了

对象不支持此属性或方法。

在线

Set outlookFolder = outlookNamespace.GetNamespace("MAPI").GetDefaultFolder(6)
电子邮件 Outlook

评论


答:

0赞 Black cat 11/14/2023 #1

outlookNamespace 已经是 MAPI。错误来自 GetNamespace 方法的重复。

    Set outlookFolder = outlookNamespace.GetNamespace("MAPI").GetDefaultFolder(6) ' 6 represents the Inbox folder

从命令中删除 GetNamespace(“MAPI”)。

    Set outlookFolder = outlookNamespace.GetDefaultFolder(6) ' 6 represents the Inbox folder