leviathanxx
New member
- Local time
- Yesterday, 22:15
- Joined
- Sep 16, 2015
- Messages
- 1
Hello everyone. As I am still learning vba please bare with me. I currently have to send out an email to individual parties with a listing of all their current items. So for example;
Customer - Item
Joe - Car
Joe - Ipad
Joe - Phone
Jake - Chainsaw
John - Ipad
John - Phone
What I would like to have is an email sent to each party listing their items. Aka their name is the distinct field and the item will be listed by row per each distinct filtered. For the above example their should be 3 emails generated in total for Joe, Jake, and John listing their items.
I managed to get most of the coding, however, I cannot seem to figure out why the code will cumulatively add items and not separate them by person. So when i run this report it exports the query to an .html (need the items listed in the body not as an attachment), then opens the export and write html to the body of the email.
It baffles me because it will generate three exported files with the persons name and the html matches the query if it were filtered by the person. However, when outlook generates the emails, it adds each filter on top of eachother. Any help would greatly be appreciated as I am so close to finishing this.
Private Sub Command0_Click()
Dim MyDB As DAO.Database, RS As DAO.Recordset
Dim strBody As String, lngCount As Long, lngRSCount As Long
DoCmd.RunCommand acCmdSaveRecord
Set MyDB = DBEngine.Workspaces(0).Databases(0)
Set RS = MyDB.OpenRecordset _
("SELECT DISTINCT Field1 FROM table1")
lngRSCount = RS.RecordCount
If lngRSCount = 0 Then
Else
RS.MoveLast
RS.MoveFirst
Do Until RS.EOF
'Changes the forms unbounded box, the query uses this to filter
Forms![Form1]![cmbfilter] = RS!Field1
Set subjecttest = RS!Field1
'Beginning of email code
Dim strline, strHTML
Dim OL As Outlook.Application
Dim MyItem As Outlook.MailItem
Set OL = New Outlook.Application
Set MyItem = Outlook.Application.CreateItem(olMailItem)
DoCmd.OutputTo acOutputQuery, "table1 Query", acFormatHTML, "C:\temp\" & RS!Field1 & ".html"
Open "C:\temp\" & RS!Field1 & ".html" For Input As 1
Do While Not EOF(1)
Input #1, strline
strHTML = strHTML & strline
Loop
Close 1
If Left(OL.Version, 2) = "10" Then
MyItem.BodyFormat = olFormatHTML
End If
If IsNull(RS!Field1) Then
Else
MyItem.Subject = subjecttest
End If
MyItem.HTMLBody = "TEST" & strHTML
MyItem.Save
RS.MoveNext
Loop
End If
RS.Close
MyDB.Close
Set RS = Nothing
Set MyDB = Nothing
Close
MsgBox "Done sending Promo email. ", vbInformation, "Done"
Exit Sub
End Sub
Customer - Item
Joe - Car
Joe - Ipad
Joe - Phone
Jake - Chainsaw
John - Ipad
John - Phone
What I would like to have is an email sent to each party listing their items. Aka their name is the distinct field and the item will be listed by row per each distinct filtered. For the above example their should be 3 emails generated in total for Joe, Jake, and John listing their items.
I managed to get most of the coding, however, I cannot seem to figure out why the code will cumulatively add items and not separate them by person. So when i run this report it exports the query to an .html (need the items listed in the body not as an attachment), then opens the export and write html to the body of the email.
It baffles me because it will generate three exported files with the persons name and the html matches the query if it were filtered by the person. However, when outlook generates the emails, it adds each filter on top of eachother. Any help would greatly be appreciated as I am so close to finishing this.
Private Sub Command0_Click()
Dim MyDB As DAO.Database, RS As DAO.Recordset
Dim strBody As String, lngCount As Long, lngRSCount As Long
DoCmd.RunCommand acCmdSaveRecord
Set MyDB = DBEngine.Workspaces(0).Databases(0)
Set RS = MyDB.OpenRecordset _
("SELECT DISTINCT Field1 FROM table1")
lngRSCount = RS.RecordCount
If lngRSCount = 0 Then
Else
RS.MoveLast
RS.MoveFirst
Do Until RS.EOF
'Changes the forms unbounded box, the query uses this to filter
Forms![Form1]![cmbfilter] = RS!Field1
Set subjecttest = RS!Field1
'Beginning of email code
Dim strline, strHTML
Dim OL As Outlook.Application
Dim MyItem As Outlook.MailItem
Set OL = New Outlook.Application
Set MyItem = Outlook.Application.CreateItem(olMailItem)
DoCmd.OutputTo acOutputQuery, "table1 Query", acFormatHTML, "C:\temp\" & RS!Field1 & ".html"
Open "C:\temp\" & RS!Field1 & ".html" For Input As 1
Do While Not EOF(1)
Input #1, strline
strHTML = strHTML & strline
Loop
Close 1
If Left(OL.Version, 2) = "10" Then
MyItem.BodyFormat = olFormatHTML
End If
If IsNull(RS!Field1) Then
Else
MyItem.Subject = subjecttest
End If
MyItem.HTMLBody = "TEST" & strHTML
MyItem.Save
RS.MoveNext
Loop
End If
RS.Close
MyDB.Close
Set RS = Nothing
Set MyDB = Nothing
Close
MsgBox "Done sending Promo email. ", vbInformation, "Done"
Exit Sub
End Sub