Hi, I have a specific VBA question. This is all good content but it's almost more than I need. I want to place a button in a form that will take all the emails in a query and place them in the "To" or "Bcc" field of an email. I have been working with the following code but with no success yet:
Private Sub Command69_Click() Dim oOutlook As Outlook.Application Dim oEmailItem As MailItem Dim rs As Recordset Dim recipientList As String If oOutlook Is Nothing Then Set oOutlook = New Outlook.Application End If
Set oEmailItem = oOutlook.CreateItem(olMailItem) With oEmailItem Set rs = CurrentDb.OpenRecordset("SELECT Email FROM ContactT") If rs.RecordCount > 0 Then rs.MoveFirst Do Until rs.EOF If IsNull(rs!Email) Then rs.MoveNext Else contactEmail = contactEmail & rs!Email & ";" .To = contactEmail rs.MoveNext End If Loop Else MsgBox "No email addresses available" End If
.To = "" .Display End With Set oEmailItem = Nothing Set oOutlook = Nothing End Sub
This code (if it worked) would send to all my contacts, which is not what I'm aiming for, but I would put in a proper query is I could get this off the ground. Thanks!
Reply from Alex Hedley:
So you have .To = contactEmail
Then near the end you have .To = ""
This will clear all your looping work.
If it's easier to follow make a string with all the To people in your loop, then set your .To to your string at the end. You can then Debug.Print(string) in various places when testing.
Sorry, only students may add comments.
Click here for more
information on how you can set up an account.
If you are a Visitor, go ahead and post your reply as a
new comment, and we'll move it here for you
once it's approved. Be sure to use the same name and email address.