DR1989
12-08-2017, 01:52 PM
I'm trying to send an email for each record in a recordset with the selected Query.
The code creates a recordset to show me how many suppliers I have in my inquiry. Next I build an SQL string sSQL for each supplier ("Lieferant"). This should be an Attachement as XLS. After that I use a Loop to create a temporary query which I have to send to each supplier. Here is my code:
Sub ExcelExportuSenden()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim qd As DAO.QueryDef
Dim sSQL As String
Dim oApp As Outlook.Application
Dim oMail As MailItem
Dim fileName As String
Set db = CurrentDb
Set qd = db.CreateQueryDef("Lieferant", "SELECT * FROM [Filter_Ausschreibung_original] WHERE 1 = 0")
Set qd = Nothing
Set rs = db.OpenRecordset( _
"SELECT DISTINCT [Lieferant] FROM [Filter_Ausschreibung_original] ", _
dbOpenForwardOnly)
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(olMailItem)
With rs
Do While Not .EOF
sSQL = "SELECT * FROM [Filter_Ausschreibung_original] " & _
" WHERE Lieferant = '" & .Fields("Lieferant") & "'"
db.QueryDefs("Anfrage").SQL = sSQL
Debug.Print sSQL
.MoveNext
Loop
.Close
End With
With oMail
.Subject = ""
.Body = "Sehr geehrte Damen und Herren," & vbCr & "" & vbCr & "anbei erhalten Sie" & _
vbCr & "" & vbCr & "- die Auftragsbestätigung für die erbrachte Dienstleistung vor Ort" & _
vbCr & "- die Prüfbescheinigungen für die wiederkehrende Prüfung vor Ort" & _
vbCr & "- die aktuelle Übersicht der Schlauchleitungen." & _
vbCr & "" & vbCr & "Die Rechnung senden wir separat an die angegebene Rechnungsadresse." & _
vbCr & "" & vbCr & "Für eventuelle Rückfragen stehen wir Ihnen zur Verfügung, gerne auch persönlich nach Terminvereinbarung." & _
vbCr & "" & vbCr & "Mit freundlichen Grüßen" & _
vbCr & "" & vbCr & ""
.Display
End With
db.QueryDefs.Delete "Lieferant"
Set rs = Nothing
Set db = Nothing
End Sub
I'm trying to send an email for each record in a recordset with the selected Query.
The code creates a recordset to show me how many suppliers I have in my inquiry. Next I build an SQL string sSQL for each supplier ("Lieferant"). This should be an Attachement as XLS. After that I use a Loop to create a temporary query which I have to send to each supplier. Here is my code:
Sub ExcelExportuSenden()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim qd As DAO.QueryDef
Dim sSQL As String
Dim oApp As Outlook.Application
Dim oMail As MailItem
Dim fileName As String
Set db = CurrentDb
Set qd = db.CreateQueryDef("Lieferant", "SELECT * FROM [Filter_Ausschreibung_original] WHERE 1 = 0")
Set qd = Nothing
Set rs = db.OpenRecordset( _
"SELECT DISTINCT [Lieferant] FROM [Filter_Ausschreibung_original] ", _
dbOpenForwardOnly)
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(olMailItem)
With rs
Do While Not .EOF
sSQL = "SELECT * FROM [Filter_Ausschreibung_original] " & _
" WHERE Lieferant = '" & .Fields("Lieferant") & "'"
db.QueryDefs("Anfrage").SQL = sSQL
Debug.Print sSQL
.MoveNext
Loop
.Close
End With
With oMail
.Subject = ""
.Body = "Sehr geehrte Damen und Herren," & vbCr & "" & vbCr & "anbei erhalten Sie" & _
vbCr & "" & vbCr & "- die Auftragsbestätigung für die erbrachte Dienstleistung vor Ort" & _
vbCr & "- die Prüfbescheinigungen für die wiederkehrende Prüfung vor Ort" & _
vbCr & "- die aktuelle Übersicht der Schlauchleitungen." & _
vbCr & "" & vbCr & "Die Rechnung senden wir separat an die angegebene Rechnungsadresse." & _
vbCr & "" & vbCr & "Für eventuelle Rückfragen stehen wir Ihnen zur Verfügung, gerne auch persönlich nach Terminvereinbarung." & _
vbCr & "" & vbCr & "Mit freundlichen Grüßen" & _
vbCr & "" & vbCr & ""
.Display
End With
db.QueryDefs.Delete "Lieferant"
Set rs = Nothing
Set db = Nothing
End
The current code does not create an email for each "Lieferant. It only starts with an email (with the first "Lieferant"), and after sending the email it doesn't open the next mail. So, i guess something is wrong with the Event "with rs" and "with oMail". I guess I should use "for each rs". Am I right?
The code creates a recordset to show me how many suppliers I have in my inquiry. Next I build an SQL string sSQL for each supplier ("Lieferant"). This should be an Attachement as XLS. After that I use a Loop to create a temporary query which I have to send to each supplier. Here is my code:
Sub ExcelExportuSenden()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim qd As DAO.QueryDef
Dim sSQL As String
Dim oApp As Outlook.Application
Dim oMail As MailItem
Dim fileName As String
Set db = CurrentDb
Set qd = db.CreateQueryDef("Lieferant", "SELECT * FROM [Filter_Ausschreibung_original] WHERE 1 = 0")
Set qd = Nothing
Set rs = db.OpenRecordset( _
"SELECT DISTINCT [Lieferant] FROM [Filter_Ausschreibung_original] ", _
dbOpenForwardOnly)
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(olMailItem)
With rs
Do While Not .EOF
sSQL = "SELECT * FROM [Filter_Ausschreibung_original] " & _
" WHERE Lieferant = '" & .Fields("Lieferant") & "'"
db.QueryDefs("Anfrage").SQL = sSQL
Debug.Print sSQL
.MoveNext
Loop
.Close
End With
With oMail
.Subject = ""
.Body = "Sehr geehrte Damen und Herren," & vbCr & "" & vbCr & "anbei erhalten Sie" & _
vbCr & "" & vbCr & "- die Auftragsbestätigung für die erbrachte Dienstleistung vor Ort" & _
vbCr & "- die Prüfbescheinigungen für die wiederkehrende Prüfung vor Ort" & _
vbCr & "- die aktuelle Übersicht der Schlauchleitungen." & _
vbCr & "" & vbCr & "Die Rechnung senden wir separat an die angegebene Rechnungsadresse." & _
vbCr & "" & vbCr & "Für eventuelle Rückfragen stehen wir Ihnen zur Verfügung, gerne auch persönlich nach Terminvereinbarung." & _
vbCr & "" & vbCr & "Mit freundlichen Grüßen" & _
vbCr & "" & vbCr & ""
.Display
End With
db.QueryDefs.Delete "Lieferant"
Set rs = Nothing
Set db = Nothing
End Sub
I'm trying to send an email for each record in a recordset with the selected Query.
The code creates a recordset to show me how many suppliers I have in my inquiry. Next I build an SQL string sSQL for each supplier ("Lieferant"). This should be an Attachement as XLS. After that I use a Loop to create a temporary query which I have to send to each supplier. Here is my code:
Sub ExcelExportuSenden()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim qd As DAO.QueryDef
Dim sSQL As String
Dim oApp As Outlook.Application
Dim oMail As MailItem
Dim fileName As String
Set db = CurrentDb
Set qd = db.CreateQueryDef("Lieferant", "SELECT * FROM [Filter_Ausschreibung_original] WHERE 1 = 0")
Set qd = Nothing
Set rs = db.OpenRecordset( _
"SELECT DISTINCT [Lieferant] FROM [Filter_Ausschreibung_original] ", _
dbOpenForwardOnly)
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(olMailItem)
With rs
Do While Not .EOF
sSQL = "SELECT * FROM [Filter_Ausschreibung_original] " & _
" WHERE Lieferant = '" & .Fields("Lieferant") & "'"
db.QueryDefs("Anfrage").SQL = sSQL
Debug.Print sSQL
.MoveNext
Loop
.Close
End With
With oMail
.Subject = ""
.Body = "Sehr geehrte Damen und Herren," & vbCr & "" & vbCr & "anbei erhalten Sie" & _
vbCr & "" & vbCr & "- die Auftragsbestätigung für die erbrachte Dienstleistung vor Ort" & _
vbCr & "- die Prüfbescheinigungen für die wiederkehrende Prüfung vor Ort" & _
vbCr & "- die aktuelle Übersicht der Schlauchleitungen." & _
vbCr & "" & vbCr & "Die Rechnung senden wir separat an die angegebene Rechnungsadresse." & _
vbCr & "" & vbCr & "Für eventuelle Rückfragen stehen wir Ihnen zur Verfügung, gerne auch persönlich nach Terminvereinbarung." & _
vbCr & "" & vbCr & "Mit freundlichen Grüßen" & _
vbCr & "" & vbCr & ""
.Display
End With
db.QueryDefs.Delete "Lieferant"
Set rs = Nothing
Set db = Nothing
End
The current code does not create an email for each "Lieferant. It only starts with an email (with the first "Lieferant"), and after sending the email it doesn't open the next mail. So, i guess something is wrong with the Event "with rs" and "with oMail". I guess I should use "for each rs". Am I right?