使用VBA将Outlook用户定义的字段导入Excel

时间:2019-10-01 16:04:14

标签: excel vba import outlook-vba

我想从Outlook导入邮件数据。

我没有问题导入经典字段,例如:发件人,主题...等。我找不到如何导入“用户定义的字段”。

用户定义的字段名为“ DemandQTY”,仅包含数字。

我从共享邮箱中获取数据。

Sub GetFromOutlook()

Dim OutApp As Outlook.Application
Dim OutNS As Namespace
Dim Folder As MAPIFolder
Dim OutMail As Variant
Dim i As Integer
Dim objOwner As Outlook.Recipient
Dim FileName As String
Dim MI As Outlook.MailItem
Dim Item As Object
Dim Atmt As Attachment

Set OutNS = GetNamespace("MAPI")
Set OutApp = New Outlook.Application
Set objOwner = OutNS.CreateRecipient("emailadress")
objOwner.Resolve

If objOwner.Resolved Then
    Set Folder = OutNS.GetSharedDefaultFolder(objOwner, olFolderInbox)

    i=2
    For Each OutMail In Folder.Items
        Sheets(2).Cells(i, 1) = OutMail.EntryID
        ' (etc....)
        Sheets(2).Cells(i, 32) = OutMail.ReminderTime    
        i = i + 1
    Next OutMail
    MsgBox "Importation Terminée"
    Sheets(2).Select
    Sheets(2).Cells(1, 1).Select
    Set OutApp = Nothing
    Set OutNS = Nothing
    Set Folder = Nothing
End If
End Sub

我尝试了Internet上发现的其他方法,但是没有用。

1 个答案:

答案 0 :(得分:0)

我们可以通过首先测试该属性是否存在来实现。如果没有,您尝试使用它,它将抛出错误。然后,如果找到该属性,我们就可以访问该值。

For Each OutMail In Folder.Items

        Sheets(2).Cells(i, 1) = OutMail.EntryID
       (etc....)
        Sheets(2).Cells(i, 32) = OutMail.ReminderTime

        If Not(OutMail.UserProperties.Find("DemandQTY", True) Is Nothing) Then    
            Sheets(2).Cells(i, 33) = OutMail.UserProperties("DemandQTY").Value
        End If

        i = i + 1
Next OutMail