【问题标题】:Get FirstName based on Alias Outlook search in vba在 vba 中根​​据别名 Outlook 搜索获取 FirstName
【发布时间】:2019-05-31 12:01:46
【问题描述】:

我可以通过以下代码进行反向操作(根据名称获取别名):是否可以根据别名获取名称? (我想在 Excel 电子表格中运行它)

Public Sub GetUsers()

Dim olApp As Outlook.Application
    Set olApp = CreateObject("Outlook.Application")
Dim olNameSpace As Outlook.Namespace
    Set olNameSpace = olApp.GetNamespace("MAPI")
Dim olAddrList As Outlook.AddressList
    Set olAddrList = olNameSpace.AddressLists("Global Address List")
Dim oGal As Outlook.AddressEntries
    Set oGal = olAddrList.AddressEntries

Dim myAddrEntry As Outlook.AddressEntry
    Set myAddrEntry = olAddrList.AddressEntries("UserA")
Dim exchUser As Outlook.ExchangeUser
    Set exchUser = myAddrEntry.GetExchangeUser

MsgBox exchUser.Alias

End Sub

基于@Dmitry Streblechenko 的建议。现在问题通过以下代码解决:

Sub GetStaffName()

Dim str As String
    str = Sheets("Form").Range("StaffID").Value
Dim olApp As Outlook.Application
    Set olApp = CreateObject("Outlook.Application")
Dim olNameSpace As Outlook.Namespace
    Set olNameSpace = olApp.GetNamespace("MAPI")
Dim olRecipient As Outlook.Recipient
    Set olRecipient = olNameSpace.CreateRecipient(str)
Dim oEU As Outlook.ExchangeUser
Dim oEDL As Outlook.ExchangeDistributionList


olRecipient.Resolve
If olRecipient.Resolved Then
    Select Case olRecipient.AddressEntry.AddressEntryUserType
        Case OlAddressEntryUserType.olExchangeUserAddressEntry
            Set oEU = olRecipient.AddressEntry.GetExchangeUser
                If Not (oEU Is Nothing) Then
                    Debug.Print oEU.PrimarySmtpAddress
                End If
            Case OlAddressEntryUserType.olExchangeDistributionListAddressEntry
                Set oEDL = olRecipient.AddressEntry.GetExchangeDistributionList
                    If Not (oEDL Is Nothing) Then
                        Debug.Print oEDL.PrimarySmtpAddress
                    End If
        End Select

    Sheets("Form").Range("StaffName").Value = oEU

End If

End Sub

【问题讨论】:

  • 你从哪里运行代码?来自 Outlook 本身,还是其他办公应用程序?
  • 嗨@Bas Verlaat,我想通过vba在excel电子表格中运行它

标签: vba outlook


【解决方案1】:

你可以用这个:

Public Function GetAliasFromName(sAddressEntry As String) As String

    With New Outlook.Application
        GetAliasFromName = .Session.AddressLists("Global Address List").AddressEntries(sAddressEntry).GetExchangeUser.Alias
    End With

End Function


Public Function GetNameFromAlias(sAlias As String) As String

    Dim oAddressEntry As Outlook.AddressEntry

    On Error Resume Next

    With New Outlook.Application
        For Each oAddressEntry In .Session.AddressLists("Global Address List").AddressEntries
            If oAddressEntry.Class = Outlook.OlObjectClass.olAddressEntry Then
                If oAddressEntry.GetExchangeUser.Alias = sAlias Then
                    GetNameFromAlias = oAddressEntry.Name
                    Exit For
                End If
            End If
        Next oAddressEntry
    End With

End Function

【讨论】:

  • 嗨,我尝试使用 Sub Test() MsgBox GetNameFromAlias("115000") End Sub 运行。虽然 If oAddressEntry.GetExchangeUser.Alias = sAlias Then 行出现运行时错误 91
  • 我已经修改了代码以检查该项目是否为地址条目。
  • 现在我想知道全球地址列表是否有问题,我可以通过检查名称按钮按别名搜索。虽然上面代码的结果给出了类似 $$All_INRHPXMB101V_Users
  • 不确定你的意思
  • 您好,第一个函数运行良好,但第二个函数 (GetNamefromAlias) 返回的值不正确?你知道错误吗?
【解决方案2】:

使用Namespace.CreateRecipient / Recipient.Resolve - 它将能够解析登录别名或姓氏。

【讨论】:

  • 嗨@Dmitry Streblechenko,有这种方法的文档和示例吗?
  • 文档没什么可做的——你只需传递一个字符串并返回一个 Recipient 对象:msdn.microsoft.com/en-us/library/office/…
  • 这是正确的答案 循环通过 GAL 是浪费时间和资源。如果AddressEntries 有一个Items 属性支持Restrict() 用于查询别名的方法,那就太好了。但由于这些功能不可用,创建收件人解析器是下一个最好的事情。在 PowerShell 中,我使用 $recipient = $namespace.CreateRecipient($user); if ($recipient.Resolve()) { return $recipient.Name }。你解决了过去几个小时一直困扰我的问题,德米特里。谢谢!
【解决方案3】:
Public Function GetNameFromAlias2(sAlias As String) As String

    Dim oAddressEntry As Outlook.AddressEntry

    On Error Resume Next

    With New Outlook.Application
     For Each oAddressEntry In .Session.AddressLists("Global Address List").AddressEntries
      If oAddressEntry.Class = Outlook.OlObjectClass.olAddressEntry Then
       If oAddressEntry.GetExchangeUser.Alias = sAlias Then
        GetNameFromAlias2 = oAddressEntry.GetExchangeUser.Alias
        Exit For
       End If
      End If
     Next oAddressEntry
    End With

End Function

@Bas Verlaat,第一个功能运行顺利,但第二个正是我需要的。但是,它没有给出正确的结果,我得到:01_New Requests 在每个单元格上。

【讨论】:

  • 永远不要遍历 GAL 容器中的所有条目 - 其中一些可以包含数万个条目。使用 Namespace.CreateRecipient / Recipient.Resolve。
猜你喜欢
  • 1970-01-01
  • 2014-02-28
  • 2018-05-27
  • 2023-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-16
相关资源
最近更新 更多