【发布时间】: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电子表格中运行它