【问题标题】:Detecting if Email is not a calendar invite检测电子邮件是否不是日历邀请
【发布时间】:2019-06-27 02:20:01
【问题描述】:

我有一些将电子邮件导入表单的代码。但是,它赶上了日历邀请这样的人。作为回应,我想过滤掉所有这些日历邀请,这样他们甚至不会一开始就尝试导入。这是我到目前为止的代码:

    Dim SenderCheck As String
'Build the list selection box
j = 0
For i = 1 To Emails.Count
    With ListBox_Emails
    If TypeName(Item) = "MailItem" Then
    SenderCheck = Emails(i).Sender.Address
    If InStr(1, SenderCheck, "express-scripts.com") > 0 Then
        .AddItem Emails(i).Sender
        .List(j, 1) = Emails(i).Subject
        .List(j, 2) = Emails(i).ReceivedTime
        .List(j, 3) = "N"
        j = j + 1
        Else: MsgBox "error"
        End If
    Else: MsgBox "not mail item"
    End If
            End With
    On Error GoTo TEMP

问题在于If TypeName(Item)="MailItem" then 行,因为现在所有内容都被视为不是邮件,我收到“不是邮件”错误。

我将如何解决这个问题?我认为语法不正确,但我不知道如何纠正它。

【问题讨论】:

  • 可能与您的主要问题无关,但您的意思是将此问题标记为“excel”而不是“outlook”吗?另外,Item 是什么?您在循环中的其他任何地方都使用Emails(i)
  • @BigBen 是的,这是在一个 excel 文件中。此外,这只是最近的一个错字。即使有Emails(i),它也不能正确过滤。
  • 你能分享你的其余代码,以获得更好的上下文吗?
  • 使用 olMail 类 - 示例 stackoverflow.com/a/42547062/4539709 或值 43,sid 答案应该有效。

标签: excel vba outlook


【解决方案1】:

这对我来说很好用。它会跳过日历邀请

Option Explicit

Sub Sample()
    Dim OutApp As Object
    Dim oMail As Object, Item As Object
    Dim objNS As Object, olFolder As Object

    Set OutApp = CreateObject("Outlook.Application")
    Set objNS = OutApp.GetNamespace("MAPI")
    Set olFolder = objNS.GetDefaultFolder(6) '<~~ olFolderInbox = 6

    For Each Item In olFolder.Items
        'https://docs.microsoft.com/en-us/office/vba/api/outlook.olobjectclass
        If Item.Class = 43 Then
            Set oMail = Item
            Debug.Print oMail.Subject
            Debug.Print oMail.SenderEmailAddress
        End If
    Next
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-02
    • 2020-11-18
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多