2016-07-25 2 views
0

사용자가 userform을 클릭 할 selectRec 단추가 있습니다.VBA를 사용하여 Outlook 주소록 대화 상자 시작

해당 버튼을 클릭하면 Outlook 주소록 대화 상자에 수신자를 추가하라는 메시지가 표시됩니다. 수신자는 ListBox1에 추가됩니다. 아래의 코드는 하나의 수신자 만 추가 할 수 있도록 허용합니다. oDialog.recipients.Item 배열의 첫 번째 항목에만 액세스하기 때문입니다. 나는 (그들이 원하는만큼 그들이 많은 이메일 주소를 추가 할 수있는) 당신은 그냥 사용할 필요가

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

내가의 길이를 알 수없는 무언가를 반복하는 루프를 사용하는 방법을 모른다 각 수신자를 통한 루프 및 루프마다

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 
관련 문제