【问题标题】:Is there any way to get the Distribution Group name of a NewEmail Event in Outlook VBA?有没有办法在 Outlook VBA 中获取 NewEmail 事件的通讯组名称?
【发布时间】:2020-06-22 14:45:03
【问题描述】:

我正在尝试在特定时间将 Outlook 中的新电子邮件从名为 Customer Service 的通讯组转移到子文件夹。我不认为规则有能力在某个时间转移电子邮件,所以我正在使用 Application.NewEmail 事件。

我现在设置了我的代码,以便它可以将电子邮件从 Exchange 发件人的电子邮件地址转移到子文件夹。但是,我需要能够以某种方式对通讯组执行相同的操作,但我不确定如何提取识别通讯组所需的信息。

这是我的代码:

Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
  Dim olApp As Outlook.Application
  Dim objNS As Outlook.NameSpace
  Set olApp = Outlook.Application
  Set objNS = olApp.GetNamespace("MAPI")
  Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub Items_ItemAdd(ByVal cusItem As Object)
    Dim olApp As Outlook.Application
    Dim objNS As Outlook.NameSpace
    Set olApp = Outlook.Application
    Set objNS = olApp.GetNamespace("MAPI")
    Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
    Dim strAddress As String, strEntryId As String
    Dim objAddressentry As Outlook.AddressEntry, objExchangeUser As Outlook.ExchangeUser
    Dim objReply As Outlook.MailItem, objRecipient As Outlook.Recipient
    Dim objDestFolder As Outlook.MAPIFolder

    If TypeName(cusItem) = "MailItem" And cusItem.SenderEmailType = "EX" Then

        On Error GoTo ErrorHandler

        Set objReply = cusItem.Reply()
        Set objRecipient = objReply.Recipients.Item(1)

        strEntryId = objRecipient.EntryID

        objReply.Close OlInspectorClose.olDiscard

        Set objAddressentry = objNS.GetAddressEntryFromID(strEntryId)
        Set objExchangeUser = objAddressentry.GetExchangeUser()

        strAddress = objExchangeUser.PrimarySmtpAddress()

        If strAddress = "jabach@example.com" And TimeValue(Now()) >= TimeValue("08:00:00 AM") And TimeValue(Now()) <= TimeValue("05:00:00 PM") Then

        Set objDestFolder = objNS.GetDefaultFolder(olFolderInbox).Folders("ryule")

        cusItem.Move objDestFolder

        End If

    End If

ProgramExit:
  Exit Sub

ErrorHandler:
  MsgBox Err.Number & " - " & Err.Description & vbCrLf & "Click Ok to continue"
  Resume ProgramExit

End Sub

Application_Startup() 也存在一些问题,它实际上并没有启动 Outlook,这就是为什么我将所有这些变量声明了两次。

【问题讨论】:

    标签: vba outlook


    【解决方案1】:

    您需要检查AddressEntry.AddressEntryUserType 属性,它从OlAddressEntryUserType 枚举中返回一个常量,表示AddressEntry 的用户类型。 AddressEntryUserType 为用户类型提供了比AddressEntry.DisplayType 更精细的粒度级别。 DisplayType 属性不区分具有不同类型 AddressEntry 的用户,例如具有简单邮件传输协议 (SMTP) 电子邮件地址、轻量级目录访问协议 (LDAP) 地址、Exchange 用户地址的 AddressEntry ,或 Outlook 联系人通讯簿中的 AddressEntry。所有这些整体都有olUser 作为他们的AddressEntry.DisplayType。例如:

    Sub DemoAE() 
     Dim colAL As Outlook.AddressLists  
     Dim oAL As Outlook.AddressList  
     Dim colAE As Outlook.AddressEntries  
     Dim oAE As Outlook.AddressEntry  
     Dim oExUser As Outlook.ExchangeUser  
     Set colAL = Application.Session.AddressLists  
     For Each oAL In colAL  
     'Address list is an Exchange Global Address List  
      If oAL.AddressListType = olExchangeGlobalAddressList Then  
       Set colAE = oAL.AddressEntries  
       For Each oAE In colAE  
         If oAE.AddressEntryUserType = _  
           olExchangeUserAddressEntry Then  
           Set oExUser = oAE.GetExchangeUser  
           Debug.Print(oExUser.JobTitle)  
           Debug.Print(oExUser.OfficeLocation)  
           Debug.Print(oExUser.BusinessTelephoneNumber)  
         End If  
       Next  
      End If  
     Next  
    End Sub
    

    因此,对于 Exchange 分发列表,您可能需要使用 AddressEntry.GetExchangeDistributionList 方法。

    OlAddressEntryUserType enumeration 提供以下常量:

    • olExchangeAgentAddressEntry - 作为 Exchange 代理的地址条目。
    • olExchangeDistributionListAddressEntry - 作为 Exchange 分发列表的地址条目。
    • olExchangeOrganizationAddressEntry - 作为 Exchange 组织的地址条目。
    • olExchangePublicFolderAddressEntry - 作为 Exchange 公用文件夹的地址条目。
    • olExchangeRemoteUserAddressEntry - 属于不同 Exchange 林的 Exchange 用户。
    • olExchangeUserAddressEntry - 属于同一 Exchange 林的 Exchange 用户。
    • olLdapAddressEntry - 使用轻量级目录访问协议 (LDAP) 的地址条目。
    • olOtherAddressEntry - 自定义或某些其他类型的地址条目,例如传真。
    • olOutlookContactAddressEntry - Outlook 联系人文件夹中的地址条目。
    • olOutlookDistributionListAddressEntry - 作为 Outlook 通讯组列表的地址条目。
    • olSmtpAddressEntry - 使用简单邮件传输协议 (SMTP) 的地址条目。

    最后,我建议处理 Application 类的 NewMailEx 事件。 NewMailEx 事件在新邮件到达收件箱时触发,在客户端规则处理发生之前。您可以使用 EntryIDCollection 数组中返回的条目 ID 来调用 NameSpace.GetItemFromID 方法并处理该项目。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-09
    • 2017-02-03
    • 2013-01-08
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多