I want to use a loop for a function that i wrote.
I have a table tbl-planung
with round about 65 entries and a listbox in my form named lstPlanung
that displays all the entries.
Each entry got an ID a CompName and some mailadresses related to the company.
ID Company Mail
1 CompName mail1@compname.com
mail2@compname.com
2 CompName2 mail1@compname2.com
mail2@compname2.com
mail3@compname2.com
I wrote a function that is creating a mail and opens Outlook with all the recepients matching the CompName.
Private Sub SendKunde_Click()
Call sendemailKunde
End Sub
Function
Sub sendemailKunde()
'Empfänger werden ausgelesen und an Outlook übergeben
Dim ThisDB As DAO.Database
Set ThisDB = CurrentDb
Dim d As DAO.Recordset
Dim q As String
q = "SELECT DISTINCT [tbl-apartner].[EMail] FROM [tbl-apartner] WHERE [tbl-apartner].[HID] = " & "'" & hid2 & "'" 'sql query
Set d = ThisDB.OpenRecordset(q, dbOpenDynaset)
Dim Result As String
Result = ""
If d.EOF = False Or d.BOF = False Then 'if-else clause
d.MoveFirst
Do While Not d.EOF
If Result <> "" Then Result = Result & "; "
Result = Result & d!EMail
d.MoveNext
Loop
End If
d.Close
'MsgBox Result 'Testausgabe
'Empfänger auslesen beendet (Variable Result beinhaltet alle Mailadressen zum Kunden)
Dim strHTML
Dim strHTMLDZ
Dim oOutlook As Outlook.Application
Dim oEmailItem As MailItem
On Error Resume Next 'verhindert Error 429 Outlook nicht geöffnet
Err.Clear
Set oOutlook = GetObject(, "Outlook.Application")
If Err.Number <> 0 Then
Set oOutlook = New Outlook.Application
End If
' Mail für Zentrale Systeme
strHTML = "<html>"
strHTML = strHTML & " <head>"
strHTML = strHTML & " </head>"
strHTML = strHTML & " <body>"
strHTML = strHTML & " <span style=""font-size: 12pt; font-family: "Arial","sans-serif";"">"
strHTML = strHTML & " MAILTEXT-1"
strHTML = strHTML & " </span>"
strHTML = strHTML & " </body>"
strHTML = strHTML & "</html>"
' Mail für dezentrale Systeme
strHTMLDZ = strHTMLDZ & "<html>"
strHTMLDZ = strHTMLDZ & "<head>"
strHTMLDZ = strHTMLDZ & "</head>"
strHTMLDZ = strHTMLDZ & "<body>"
strHTMLDZ = strHTMLDZ & " <span style=""font-size: 12pt; font-family: "Arial","sans-serif";"" > "
strHTMLDZ = strHTMLDZ & " MAILTEXT-2"
strHTMLDZ = strHTMLDZ & " </span>"
strHTMLDZ = strHTMLDZ & " </body>"
strHTMLDZ = strHTMLDZ & "</html>"
Set oEmailItem = oOutlook.CreateItem(olMailItem)
With oEmailItem
'.CC = "TEST@TEST.de" <- optional
'.To = Me.mail2 <- Empfänger = TextBox mail2
.SentOnBehalfOfName = "MAIL@DOM.DE"
.To = Result
.Subject = "SAP Patche - " & sid2 & " am: " & datum2 & " um: " & uhr2 & " Uhr"
If zentral2 < 0 Then
.HTMLBody = strHTML
Else
.HTMLBody = strHTMLDZ
End If
.Display
End With
Set oEmailItem = Nothing
Set oOutlook = Nothing
End Sub
Is it possible to wrap my function into a loop that i dont have to select every entry in my listbox manually?
I though about a button and a function sendemailAll
thats opens automatically a new mail in Outlook for each id in my listbox.
At this moment i have to select every entry in the listbox, click a button and send the mail via outlook.
EDIT :
I thought about something like:
Mail1
ID:1 CompName TO: mail1@compname.com; mail2@compname.com
Mail2
ID2: CompName2 TO: mail1@compname2.com; mail2@compname2.com; mail3@compname2.com
The solution from R3uK looks like this:
Mail1
ID:1 CompName TO: mail1@compname.com
Mail2
ID:1 CompName TO: mail1@compname.com; mail2@compname.com
Mail3
ID2: CompName2 TO: mail1@compname2.com
Mail4
ID2: CompName2 TO: mail1@compname2.com; mail2@compname2.com
Mail5
ID2: CompName2 TO: mail1@compname2.com; mail2@compname2.com; mail3@compname2.com