2016-07-25 32 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