本文介绍了使用VBA将Outlook用户定义的字段导入Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Outlook导入邮件数据.

I would like to import mail data from Outlook.

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

I have no problem to import classic fields such as: From, Subject ... etc. I cannot find how to import my "User-defined field".

用户定义的字段名为"DemandQTY",仅包含数字.

The User-defined field is named "DemandQTY" and contains only numbers.

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

I get my data from a shared mailbox.

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上发现的其他方法,但是没有用.

I tried different methods found on internet, but nothing worked.

推荐答案

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

We can do this by first testing if the property exists. If it doesn't and you try to work with it, it will throw an error. Afterwards we can access the value if the property is found.

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

这篇关于使用VBA将Outlook用户定义的字段导入Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 08:59