2016-07-25 102 views
0

我有一個名爲selectRec的按鈕,用戶將在用戶窗體中單擊。使用VBA啓動Outlook通訊簿對話框

我想要點擊該按鈕,然後他們的Outlook地址簿對話框提示他們添加收件人。收件人將被添加到ListBox1。下面的代碼只允許添加一個收件人,即使選擇了很多收件人。這是因爲它只訪問oDialog.recipients.Item陣列中的第一項。我不知道如何使用for循環的東西遍歷我不知道的長度(因爲他們可以添加多個電子郵件地址,因爲他們想)

Private Sub selectRec_Click() 

    Dim olApp As Outlook.Application 
    Dim oDialog As SelectNamesDialog 
    Dim oGAL As AddressList 
    Dim myAddrEntry As AddressEntry 
    Dim exchUser As Outlook.ExchangeUser 

    Dim AliasName As String 
    Dim FirstName As String 
    Dim LastName As String 
    Dim EmailAddress As String 

    Set olApp = GetObject(, "Outlook.Application") 
    Set oDialog = olApp.Session.GetSelectNamesDialog 
    Set oGAL = olApp.GetNamespace("MAPI").AddressLists("Global Address List") 

    With oDialog 
     .AllowMultipleSelection = True 
     .InitialAddressList = oGAL 
     .ShowOnlyInitialAddressList = True 



     If .Display Then 

      AliasName = oDialog.recipients.Item(1).Name 

      Set myAddrEntry = oGAL.AddressEntries(AliasName) 
      Set exchUser = myAddrEntry.GetExchangeUser 

      If Not exchUser Is Nothing Then 
       FirstName = exchUser.FirstName 
       LastName = exchUser.LastName 
       EmailAddress = exchUser.PrimarySmtpAddress 

      End If 
      ListBox1.AddItem (EmailAddress) 



     End If 
    End With 
Set olApp = Nothing 
Set oDialog = Nothing 
Set oGAL = Nothing 
Set myAddrEntry = Nothing 
Set exchUser = Nothing 
End Sub 

回答

0

你只需要使用a通過每個迴路並循環通過每個收件人

If .Display Then 

    Dim userSelected As Outlook.Recipient 
     For Each userSelected In .Recipients 

      AliasName = userSelected.Name 

      Set myAddrEntry = oGAL.AddressEntries(AliasName) 
      Set exchUser = myAddrEntry.GetExchangeUser 

      If Not exchUser Is Nothing Then 
       FirstName = exchUser.FirstName 
       LastName = exchUser.LastName 
       EmailAddress = exchUser.PrimarySmtpAddress 

      End If 
      ListBox1.AddItem (EmailAddress) 

     Next 
    End If