【问题标题】:Outlook MailItem - Determine if the item is sent or receivedOutlook MailItem - 确定项目是发送还是接收
【发布时间】:2020-05-17 10:12:24
【问题描述】:

在 VBA 中,如何识别 Outlook MailItem 是已发送还是已接收?

【问题讨论】:

    标签: vba outlook mailitem


    【解决方案1】:

    没有好的方法可以做到这一点,唯一的解决方法是检查MailItem.ReceivedByName 属性(或任何其他ReceivedBy* 属性)- 它会为来自已发送邮件文件夹的邮件返回一个空字符串当前用户。对于收到的消息,它会返回一个非空值。

    【讨论】:

    • 我听说ReceivedByName 在发件人不在公司范围内时不起作用(因此只有电子邮件),但必须验证这一点...
    • ReceivedByXYZ 属性与发送者无关 - 它们与用户接收消息有关。请记住,Outlook 用户可以有多个身份(例如,在将多个 POP3 帐户交付到同一个商店的情况下)。
    • 如果我为已发送文件夹中的项目打印item.ReceivedByName,即使我不是收件人之一,它们也会注明我的姓名。我收到的物品,也注明了我的名字(如预期的那样)。所以ReceivedByName 似乎根本无法按我的要求运行(Office 2013)
    • 这就是您在 Outlook Spy 中看到的(单击项目按钮,选择 ReceivedByName 属性)?消息是如何发送的?在我的环境中,对于 Exchange 和 POP3/SMTP 帐户,该属性为空。
    • 不确定 Outlook Spy。我只是使用 VBA 来打印所选项目的属性。我还检查了本地窗口(我假设它类似于 Outlook Spy),它确认了我的数据。虽然不确定我们的 IT 使用什么环境...
    【解决方案2】:

    这似乎很笼统:

    Public Enum MailItemStatus
      Sent
      Received
      Draft
    End Enum
    
    Public Function getMailStatus(mItem as Mailitem) as MailItemStatus
      if not mItem.sender is nothing then
        If mItem.Sender.Address = Application.Session.CurrentUser.Address Then
          if mItem.sent then
            'Item is sent
            getMailStatus = MailItemStatus.Sent
          else
            'Item is draft
            getMailStatus = MailItemStatus.Draft
          end if
        else
          'Item is received
          getMailStatus = MailItemStatus.Received
        end if
      else
        'Item is draft???
        getMailStatus = MailItemStatus.Draft
      end if
    end function
    

    【讨论】:

    • 将不起作用的代码放入问题中。可能的响应者可以修复您的非工作代码并将该固定代码放在答案帖子中。也许如果你自己删除了这篇文章,它仍然会被删除。
    • @niton 就我而言,这是可行的,因此是一个答案。可能有更直接的方法更有利,但这并不影响作为答案的有效性。
    • if mItem.sender is nothing then 你怎么能尝试在下一行得到mItem.Sender.Address
    • 糟糕,确实如此。感谢您指出错误。昨晚写的时候没发现
    【解决方案3】:
    
    Public Enum MailItemStatus
      Sent
      Received
      Draft
    End Enum
    
    
    ' Following the poster's Enum idea:
    
    Public Function getMailStatus(mItem As MailItem) As MailItemStatus
    
        If mItem.Sent Then
    
            If mItem.Sender.Address = Session.CurrentUser.Address Then
                getMailStatus = MailItemStatus.Sent
    
            Else    'Item sent by someone who is not you
                getMailStatus = MailItemStatus.Received
            End If
    
        Else    'Item has not been sent
    
            getMailStatus = MailItemStatus.Draft
    
        End If
    
    End Function
    

    【讨论】:

    • 直接使用 mItem.sent 而不是检查 mItem.sender 是有意义的。感谢您简化 :) 我确实还发现有 3 种状态。 mItem.SavedmItem.PostedmItem.Sent 我不确定 posted 是什么意思……
    猜你喜欢
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    • 2014-07-23
    相关资源
    最近更新 更多