Email to multiple recipients based on listbox (1 Viewer)

GeminiG

New member
Local time
Today, 14:53
Joined
Aug 19, 2016
Messages
9
Hi All, I'm assuming the above should be fairly simple but I can't seem to get the "TO:" in my VBA to populate the selections based on a list box. I'm a beginner user. Can somebody please post an example of how I can refer to the listbox results in code? Many thanks in advance for any help you can provide.
 

Ranman256

Well-known member
Local time
Today, 17:53
Joined
Apr 9, 2015
Messages
4,337
Loop thru the list box,grab the item, then send.
Code:
Dm sTo as string
For I = 0 to listBox.listcount-1
   STo = Listbox.itemdata(I)     'Get next item in list
   ListBox = sTo                      'Set listbox to that item
   Docmd.sendTo .....
Next
 

Cronk

Registered User.
Local time
Tomorrow, 07:53
Joined
Jul 4, 2013
Messages
2,774
That would generate a separate email to everyone in the list box, not the selected ones.

The original request was not clear as to whether separate emails were to be generated on just one email with every selected address included.
 

GeminiG

New member
Local time
Today, 14:53
Joined
Aug 19, 2016
Messages
9
Sorry I do want to send just ONE email to only the selected email addresses in the listbox. The list box is based on a previous combo box query. All the code examples I can find only send to all email addresses in a table or distribution list. I want to specifically send only to emails the user selects in a listbox. Thank you
 

Cronk

Registered User.
Local time
Tomorrow, 07:53
Joined
Jul 4, 2013
Messages
2,774
Code:
      strEmailRecipients = ""
      For n = 0 To Me!YourListBox.ListCount - 1
         If Me!YourListBox.Selected(n) = True Then
            strEmailRecipients = strEmailRecipients & "; " & Me!YourListBox.Column(2, N)   'change 2 to column number containing the address
         End If
      Next n
      strEmailRecipients = Mid(strEmailRecipients, 3)
 

GeminiG

New member
Local time
Today, 14:53
Joined
Aug 19, 2016
Messages
9
Thanks Cronk I'm getting closer but I don't think I'm putting the code in the right place. Here is my code, i've also attached the database.

Code:
Private Sub cmdSendTask_Click()
Dim outlookApp As Outlook.Application
Dim outlookTask As Outlook.TaskItem
Set outlookApp = CreateObject("outlook.application")
Set outlookTask = outlookApp.CreateItem(olTaskItem)
    
    strEmailRecipients = ""
      For n = 0 To Me!lbEmail.ListCount - 1
         If Me!lbEmail.Selected(n) = True Then
            strEmailRecipients = strEmailRecipients & "; " & Me!lbEmail.Column(2, n)   'change 2 to column number containing the address
         End If
      Next n
      strEmailRecipients = Mid(strEmailRecipients, 3)
      
With oMsg
.To = ("strEmailRecipients")
End With

With outlookTask
.Subject = "Reminder for Task: " & Me.TaskName
.Body = "This is a reminder 3 days before due. Task name: " & Me.TaskName & " is due on " & Me.DueDate
.ReminderSet = True
.DueDate = Me.DueDate
'3 days prior reminder
.ReminderTime = Me.DueDate - 3 & " 8:00AM"
ReminderPlaySound = True
.Save

End With
    MsgBox "Task has been sent to Outlook successfully.", vbInformation, "Set Task Confirmed"
End Sub
 

Attachments

  • Tasks.accdb
    704 KB · Views: 165

Users who are viewing this thread

Top Bottom