首页 > 解决方案 > 有没有办法在 Outlook VBA 中获取 NewEmail 事件的通讯组名称?

问题描述

我试图在特定时间将 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,这就是为什么我将所有这些变量声明了两次。

标签: vbaoutlook

解决方案


您需要检查AddressEntry.AddressEntryUserType属性,该属性从OlAddressEntryUserType枚举中返回一个常量,表示AddressEntry的用户类型。AddressEntryUserType为用户类型提供比AddressEntry.DisplayType. 该DisplayType属性不区分具有不同类型的用户AddressEntry,例如AddressEntry具有简单邮件传输协议 (SMTP) 电子邮件地址、轻量级目录访问协议 (LDAP) 地址、Exchange 用户地址或AddressEntryOutlook 联系人通讯簿中的用户. 所有这些整体都有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枚举提供以下常量:

  • 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方法并处理该项目。


推荐阅读