本文介绍了在vba中基于Alias Outlook搜索获取名字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以通过以下代码进行反向操作(基于名称获取别名):是否可以基于别名获取名称? (我想在excel电子表格中运行它)
I able to do reverse (Get Alias based on Name) by following code: Is it possible to get Name based on Alias ? (I would like to run it in excel spreadsheet)
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的建议.现在问题可以通过以下代码解决:
Based on @Dmitry Streblechenko suggestion. Now problem resolved by following code:
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
推荐答案
使用Namespace.CreateRecipient
/Recipient.Resolve
-它将能够解析登录别名或姓氏.
Use Namespace.CreateRecipient
/ Recipient.Resolve
- it will be able to resolve both a login alias or a last name.
这篇关于在vba中基于Alias Outlook搜索获取名字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!