excel - 从未读电子邮件中复制数据并标记为已读

标签 excel vba email outlook

我想将 Outlook RSS 源中的“未读电子邮件”复制到 Excel。这些复制的电子邮件应在 Outlook 中标记为“已读”。
下面的代码返回

Invalid procedure call or argument.


Private Sub run_btn_Click()
    Dim OutlookApp As Outlook.Application
    Dim OutlookNamespace As Namespace
    Dim Folder As MAPIFolder
    Dim OutlookMail As Variant

    Dim i As Integer

    Set OutlookApp = New Outlook.Application
    Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
    Set Folder = OutlookNamespace.GetDefaultFolder(olfolderrssfeeds).Folders("Folder Name")

    If Folder.items.Restrict("[UnRead] = True").Count = 0 Then
        MsgBox "No Unread email", vbInformation, "Congratulation!"
    End If

    i = 1

    For Each OutlookMail In Folder.items.Restrict("[UnRead] = True")
        Range("eMail_subject").Offset(i, 0).Value = Left(OutlookMail.Subject, 11)
        Range("eMail_date").Offset(i, 0).Value = OutlookMail.ReceivedTime
        Range("eMail_text").Offset(i, 0).Value = OutlookMail.Body        
        i = i + 1
    Next OutlookMail

    If Folder.items.Restrict("[Unread] = True") Then
        Folder.items.UnRead = False
        Folder.items.Save
    End If

    Set Folder = Nothing
    Set OutlookNamespace = Nothing
    Set OutlookApp = Nothing
End Sub

最佳答案

我无法重现您看到的确切错误,而且我不知道错误在哪里。但是,以下对我有用,从 Excel 2013 运行以控制 Outlook 2013。请参阅 <==分数。

Option Explicit    ' <== Always include this at the top of every module

Private Sub run_btn_Click()
    Dim OutlookApp As Outlook.Application
    Dim OutlookNamespace As Namespace
    Dim Folder As MAPIFolder
    Dim OutlookMail As Object   ' <== Doesn't need to be Variant

    Dim rowIndex As Integer     ' <== rename from `i` to `rowIndex` for clarity

    Set OutlookApp = New Outlook.Application
    Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
    Set Folder = OutlookNamespace.GetDefaultFolder(olFolderRssFeeds)        ' <==
        ' After you call GetDefaultFolder, you already have a folder - you don't
        ' need to call .Folder() on it.

    If Folder.UnReadItemCount = 0 Then      ' <== Don't need to use Restrict for unread-item count
        MsgBox "No Unread email", vbInformation, "Congratulation!"
    End If

    rowIndex = 1

    For Each OutlookMail In Folder.Items.Restrict("[UnRead] = True")
        Range("eMail_subject").Offset(rowIndex, 0).Value = Left(OutlookMail.Subject, 11)
        Range("eMail_date").Offset(rowIndex, 0).Value = OutlookMail.ReceivedTime
        Range("eMail_text").Offset(rowIndex, 0).Value = OutlookMail.Body

        MarkItemReadIfEmail OutlookMail     ' <== Mark each one read as it's processed

        rowIndex = rowIndex + 1
    Next OutlookMail

    'If Folder.UnReadItemCount > 0 Then     ' <== already did this in the loop above
    '    Folder.Items.UnRead = False        '     so don't need to do it here.
    '    Folder.Items.Save
    'End If

    Set Folder = Nothing
    Set OutlookNamespace = Nothing
    Set OutlookApp = Nothing
End Sub

Private Sub MarkItemReadIfEmail(obj As Object)
    Dim mail As PostItem    ' **Edit** - was originally MailItem

    ' Find out if it's a mail item
    Set mail = Nothing
    On Error Resume Next
    Set mail = obj
    On Error GoTo 0

    If mail Is Nothing Then Exit Sub

    ' It's an email, so mark it.
    mail.UnRead = False
    mail.Save
End Sub
Sub MarkItemReadIfEmail是一种标记电子邮件已读的谨慎方式。我实际上对 Outlook 对象模型了解得不够多,无法知道 Folder.Items总是返回 编辑 PostItem用于 RSS 提要文件夹。因此,在将每个项目视为 PostItem 之前,我检查它是否真的是一个。

关于excel - 从未读电子邮件中复制数据并标记为已读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56019649/

相关文章:

vba - Excel VBA 性能编码设置

python - 确保从 Excel 运行批处理文件时在 cmd.exe 中使用相对路径

JavaMail 无效的 MSGID

python - 发送邮件时Ascii编码错误

iphone - 以编程方式通过电子邮件发送 iPhone 附件

c# - 如何在 C# 中编写调用 C++ DLL 导出函数的 excel 插件

sql-server - 在 OLAP 多维数据集中,过滤属性时总计错误

vba - 如何将类名作为参数传递?

vba - 将 VBA 代码移动到独立的 VBS 文件

vba - For-Next 是否有更有效的方法,包括 If-Then 语句?