Donnie1ben
New member
- Local time
- Today, 20:35
- Joined
- Aug 15, 2022
- Messages
- 17
I have this table and would like to send one email to Names of individuals who share same country.
Thus Adam and Mina will receive one email in Table format as well as same for Graeme and Briann
I found a sample solution online but encounter the "End if without block if" when I implement same code. I tried countless techniques I found online but none seems to work.
The highlighted "End if" gets highlighted in debug mode
See sample code:
strEmail = ""
strCountry = rec!Country
End If
Next
End If
Name | Country | City | School | |
Adam | TRNC | Lefkosa | CIU | Adam@mail.com |
Mina | TRNC | Lefkosa | NEU | Mina@mail.com |
Graeme | USA | Missouri | MUNI | Graeme@mail.com |
Briann | USA | Nebraska | NBUNI | Briann@mail.com |
Betty | UK | Hull | HUNI | Betty@mail.com |
Thus Adam and Mina will receive one email in Table format as well as same for Graeme and Briann
Name | Country | City | School | |
Adam | TRNC | Lefkosa | CIU | Adam@mail.com |
Mina | TRNC | Lefkosa | NEU | Mina@mail.com |
I found a sample solution online but encounter the "End if without block if" when I implement same code. I tried countless techniques I found online but none seems to work.
The highlighted "End if" gets highlighted in debug mode
See sample code:
Code:
Public Sub NewEmail()
Dim olApp As Object
Dim olItem As Variant
Dim rec As DAO.Recordset
Dim aRow(1 To 5) As String
Dim aBody() As String
Dim lCnt As Long
Dim strCountry As String
Dim strHTML As String
Dim strEmail As String
Dim x As Integer
strHTML = "<HTML><body><table border='2'><tr><th>"
strHTML = strHTML & "Name</th><th>"
strHTML = strHTML & "Country</th><th>"
strHTML = strHTML & "City</th><th>"
strHTML = strHTML & "School</th><th>"
strHTML = strHTML & "Email</th>"
strHTML = strHTML & "XXX</table></body></html>"
lCnt = 1
Set olApp = CreateObject("Outlook.application")
Set rec = CurrentDb.OpenRecordset("SELECT * FROM DummyTable ORDER BY Country, City, School, Name")
If Not (rec.BOF And rec.EOF) Then
strCountry = rec!Country
For x = 1 To rec.recordCount
lCnt = lCnt + 1
ReDim Preserve aBody(1 To lCnt)
aRow(1) = rec("Name") & "</td><td>"
aRow(2) = rec("Country") & "</td><td>"
aRow(3) = rec("City") & "</td><td>"
aRow(4) = rec("School") & "</td><td>"
aRow(5) = rec("Email") & "</td>"
aBody(lCnt) = "<tr><td>" & Join(aRow, "") & "</tr>"
strEmail = strEmail & rec!Email & ";"
If x < rec.recordCount Then rec.MoveNext
If x = rec.recordCount Or strCountry <> rec!Country Then
'create the email
Set olItem = olApp.CreateItem(0)
olItem.Display
olItem.To = "my email"
olItem.BCC = strEmail
olItem.Subject = "Test E-mail"
olItem.HTMLBody = Replace(strHTML, "xxx", Join(aBody, vbNewLine))
olItem.Display
lCnt = 0
strEmail = ""
strCountry = rec!Country
End If
Next
End If
strEmail = ""
strCountry = rec!Country
End If
Next
End If
Last edited: