2015-10-05 68 views
2

我能夠通過以下代碼進行反向操作(基於名稱獲取別名):是否可以根據別名獲取Name? (我想運行它在Excel電子表格中)獲取姓名基於別名Outlook在VBA搜索

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 
+0

你從哪裏運行代碼?從展望本身,還是不同的辦公應用程序? –

+0

嗨@Bas Verlaat,我想運行它在Excel電子表格通過vba – useR

回答

1

您可以使用此:

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 
+0

嗨,我試圖運行使用Sub Test()MsgBox GetNameFromAlias(「115000」)End Sub。如果在該行有運行時error91如果oAddressEntry.GetExchangeUser.Alias = sAlias然後 – useR

+0

我修改了代碼來檢查該項是否是地址條目。 –

+0

現在我想知道全局地址列表是否有問題,我可以通過檢查名稱按鈕進行別名搜索。雖然上面的代碼給出的結果就像$$ ALL_INRHPXMB101V_Users – useR

1

使用Namespace.CreateRecipient/Recipient.Resolve - 這將是能夠爲登錄別名或最後都解決名稱。

+0

嗨@Dmitry Streblechenko,有沒有這種方法的任何文檔和示例? – useR

+1

文檔沒有太多要做的事情 - 只傳遞一個字符串並找回收件人對象:https://msdn.microsoft.com/en-us/library/office/ff866418.aspx?f=255&MSPPError=-2147217396 –

+0

這是正確的答案通過GAL循環是浪費時間和資源。如果'AddressEntries'有一個'Items'屬性支持用來查詢別名的'Restrict()'方法,那就好了。但由於這些功能不可用,因此創建收件人解析器是次佳選擇。在PowerShell中,我做'$ recipient = $ namespace.CreateRecipient($ user); if($ recipient.Resolve()){return $ recipient.Name}'。你解決了一個問題,在過去的幾個小時德米特里嘮叨我。謝謝! – rojo