VBA Outlook Mailitem - 不显示所有项目

时间:2014-06-02 21:08:32

标签: excel vba outlook populate mailitem

以下代码无法收集收件箱中的所有电子邮件。

我的列表框中的第一项是来自昨天和2014年4月22日的电子邮件 - 虽然我的邮箱包含的内容远不止于此。

Sub CheckEmail()

On Error Resume Next

Dim outApp As Outlook.Application
Dim outNs As Outlook.Namespace
Dim outFldr As Outlook.MAPIFolder
Dim outEmail As Outlook.MailItem

Dim p As Integer
p = 0

Set outApp = CreateObject("Outlook.Application")
Set outNs = outApp.GetNamespace("MAPI")
Set outFldr = outNs.GetDefaultFolder(olFolderInbox)

Dim searcht As String

'find search string

' do search

        For Each outEmail In outFldr.Items

            With fmShowsInboxEmails.ListBox1
                .AddItem outEmail.EntryID
                .List(p, 1) = outEmail.ReceivedTime
                .List(p, 2) = outEmail.Subject
                .List(p, 3) = outEmail.SenderEmailAddress
                .List(p, 4) = outEmail.Attachments.Count
            End With

            p = p + 1

        Next outEmail

On Error GoTo 0

Set outApp = Nothing
Set outNs = Nothing
Set outFldr = Nothing
Set outEmail = Nothing

fmShowsInboxEmails.Show

End Sub

1 个答案:

答案 0 :(得分:1)

“下一个outEmail上的类型不匹配”

收件箱中的项目不一定是邮件项目。

一旦你有Dim outEmail As Variant,在添加到列表框之前测试outEmail是mailitem。