gpt4 book ai didi

vba - 在 vba 中根​​据别名 Outlook 搜索获取名字

转载 作者:行者123 更新时间:2023-12-04 16:20:01 28 4
gpt4 key购买 nike

我可以通过以下代码进行反向操作(根据名称获取别名):Is it possible to get Name based on Alias ? (我想在 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

最佳答案

你可以使用这个:

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

关于vba - 在 vba 中根​​据别名 Outlook 搜索获取名字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32943435/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com