在Outlook中查看某人的聯繫人名片時,會出現Office的字段以顯示其位置。我怎樣才能找到使用VBA?這裏是我最實用的代碼:在VBA中查找交換用戶的辦公位置
Private Function getLocation(username As String) As String
Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olGAL As Outlook.AddressEntries
Dim olAddressEntry As Outlook.AddressEntry
Dim olUser As Outlook.ExchangeUser
Set olApp = New Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olGAL = olNS.AddressLists("Global Address List").AddressEntries
Set olAddressEntry = olGAL.Item(username)
Set olUser = olAddressEntry.GetExchangeUser
Debug.Print olGAL.Count 'count is 646718
Debug.Print olUser.OfficeLocation
Debug.Print olUser.Address
Debug.Print olUser.Name
getLocation = olUser.OfficeLocation
Set olApp = Nothing
Set olNS = Nothing
Set olGAL = Nothing
Set olAddressEntry = Nothing
Set olUser = Nothing
End Function
這工作,當我搜索自己的實際名稱(例如,約翰·史密斯),但它只會返回前約翰·史密斯。我如何使用他們的電子郵件地址或別名進行搜索?
注意:我添加了對Microsoft Outlook 16.0 Object Library
的引用以充分利用Intellisense,但我計劃在其工作後切換到後期綁定。