Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sending e-mails to multiple addresses using list box 1

Status
Not open for further replies.

neerajam

Technical User
Mar 4, 2002
23
0
0
US
I would like to send the same e-mail message to multiple addresses using a list box. I have been successful sending it to a single address but am struggling with multiple addresses. I would sincerely appreciate any help. Thanks.

Code used:
Dim ol As New Outlook.Application
Dim ns As Outlook.NameSpace
Dim newMail As Outlook.MailItem
Dim i As Integer
'Return a reference to the MAPI layer.
' Set ns = ol.GetNamespace("MAPI")

'Create a new mail message item.
Set newMail = ol.CreateItem(olMailItem)
For i = 0 To List4.ListCount - 1
With newMail
.Subject = "Training Information for October 1997"
.Body = "Here is the training information you
requested:" & vbCrLf

'Add a recipient and test to make sure that the
'address is valid using the Resolve method.

.Recipients.Add(List4.Selected(i))
.Type = olTo
If Not .Resolve Then
MsgBox "Unable to resolve address.",
vbInformation
Exit Sub
End If
.Send
End With
i = i + 1
Next

'Release memory.
Set ol = Nothing
' Set ns = Nothing
Set newMail = Nothing
 
I think you would be better off declaring an array variable, then step through the ItemsSelected collection and assign each address to a subscript of the array, then build your message and use the array to build your recipients list. Maybe something like this:

Code:
'declare the array
Dim aryRecipList() As String
Dim i As Integer

'set the dimensions of the array
ReDim aryRecipList(List4.ListCount - 1)

'populate the array
For i = 0 to UBound(aryRecipList)
    aryRecipList(i) = List4.ItemData(i)
Next i

'create new mail message item
Set newMail = ol.CreateItem(olMailItem)
With newMail
    .Subject = "Training Information for October 1997"
    .Body = "Here is the training information you requested:" & vbCrLf
    'step through the array to add recipients
    For i = 0 to UBound(aryRecipList)
        .Recipients.Add(aryRecipList(i))
        If Not .Resolve Then
            MsgBox "Unable to resolve " & aryRecipList(i) & vbCrLf & "Action cancelled.", vbInformation
            Exit Sub
        End If
    Next i
    .Send
End With
This is untested, I just cobbled it together off the top of my head, but may get you pointed in the right direction.

HTH

Ken S.
 
Oops, I left out the ".Type = olTo" line...

Here's a corrected version:

Code:
'declare the array
Dim aryRecipList() As String
Dim i As Integer

'set the dimensions of the array
ReDim aryRecipList(List4.ListCount - 1)

'populate the array
For i = 0 to UBound(aryRecipList)
    aryRecipList(i) = List4.ItemData(i)
Next i

'create new mail message item
Set newMail = ol.CreateItem(olMailItem)
With newMail
    .Subject = "Training Information for October 1997"
    .Body = "Here is the training information you requested:" & vbCrLf
    'step through the array to add recipients
    For i = 0 to UBound(aryRecipList)
        .Recipients.Add(aryRecipList(i))
        [b].Type = olTo[/b]
        If Not .Resolve Then
            MsgBox "Unable to resolve " & aryRecipList(i) & vbCrLf & "Action cancelled.", vbInformation
            Exit Sub
        End If
    Next i
    .Send
End With

Ken S.
 
Thanks Eupher for the prompt and excellent reply.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top