基于vba中的Alias Outlook搜索获取名字
我可以通过以下代码进行反向操作(基于名称获取别名):是否可以基于别名获取名称?(我想在excel电子表格中运行它) 基于@Dmitry Streblechenko的建议。现在,问题已通过以下代码解决:基于vba中的Alias Outlook搜索获取名字,vba,outlook,Vba,Outlook,我可以通过以下代码进行反向操作(基于名称获取别名):是否可以基于别名获取名称?(我想在excel电子表格中运行它) 基于@Dmitry Streblechenko的建议。现在,问题已通过以下代码解决: Sub GetStaffName() Dim str As String str = Sheets("Form").Range("StaffID").Value Dim olApp As Outlook.Application Set olApp = CreateObject("
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
您可以使用以下选项:
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
使用
Namespace.CreateRecipient
/Recipient.Resolve
-它将能够解析登录别名或姓氏
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_新请求
在每个单元格上。从何处运行代码?从outlook本身或其他office应用程序?您好@Bas Verlaat,我想通过vbaHI在excel电子表格中运行它,我尝试使用Sub Test()MsgBox GetNameFromAlias(“115000”)运行它End Sub.如果oAddressEntry.GetExchangeUser.Alias=sAlias,行中出现运行时错误91,则我已修改代码以检查该项是否为地址项。现在我想知道全局地址列表是否有问题,我可以通过“检查名称”按钮按别名搜索。虽然上面代码的结果给出了类似于$$All_INRHPXMB101V_users的东西不确定您的意思,但是第一个函数工作正常,而第二个函数(GetNamefromAlias)返回的值不正确?您是否注意到错误?嗨@Dmitry Streblechenko,是否有此方法的文档和示例?文档中没有太多要做的事情-您只需传递一个字符串并返回一个收件人对象:这是正确的答案,在GAL中循环是浪费时间和资源的。如果
AddressEntries
有一个Items
属性,支持查询别名的Restrict()
方法,那就太好了。但由于这些功能不可用,因此创建收件人解析程序是次好的选择。在PowerShell中,我执行$recipient=$namespace.CreateRecipient($user);如果($recipient.Resolve()){return$recipient.Name}
。你解决了过去几个小时一直困扰我的一个问题。非常感谢。永远不要循环一个GAL容器中的所有条目——其中一些可以包含成千上万个条目。使用Namespace.CreateRecipient/Recipient.Resolve。
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