Option Explicit
' Beispiel mit Early Binding
' Verweis Microsoft Outlook 16.0 Library muss aktiviert sein (in diesem Beispiel bereit aktiviert)
Sub TestMail()
Dim Outobj As Outlook.Application
Dim Mail As Object
Dim arrAdr(): arrAdr = Tabelle1.UsedRange.Value
Dim i&
Set Outobj = New Outlook.Application
For i = LBound(arrAdr) + 1 To UBound(arrAdr)
With Outobj.CreateItem(0)
.GetInspector.Display ' öffnet den erzeugten Item
.To = arrAdr(i, 1)
.CC = arrAdr(i, 2)
.Subject = arrAdr(i, 4)
.htmlBody = "Hallo! " & arrAdr(i, 3) & "
" & arrAdr(i, 5) & "
" & TableHTMLtoBody(Tabelle1.Range("rng_Info")) & "
Mit freundlichen Grüßen
Dein Name
"
'.Send
End With
Next i
Set Outobj = Nothing
End Sub
Function TableHTMLtoBody(xlRange As Range) As String
Dim i&, j&, htmlBody$, Zelle$
htmlBody = ""
' Überschriften (ggf. anpassen)
For i = 1 To xlRange.Columns.Count
htmlBody = htmlBody & "" & xlRange.Cells(1, i).Value & " "
Next i
' Datenzellen
htmlBody = htmlBody & ""
For i = 2 To xlRange.Rows.Count
htmlBody = htmlBody & ""
For j = 1 To xlRange.Columns.Count
Zelle = xlRange.Cells(i, j).Value
If IsEmpty(Zelle) Then
Zelle = " "
End If
htmlBody = htmlBody & "" & Zelle & " "
Next j
htmlBody = htmlBody & " "
Next i
htmlBody = htmlBody & "
"
TableHTMLtoBody = htmlBody
End Function
Option Explicit
' Beispiel mit Early Binding
' Verweis Microsoft Outlook 16.0 Library muss aktiviert sein (im diesem Beispiel bereit aktiviert)
Sub MailVersenden()
Dim Outobj As Outlook.Application
Dim Mail As Object, oDic As Object
Dim i&, j&, k&, tmp, arrAdrZ(), arrAdr()
Set oDic = CreateObject("Scripting.Dictionary")
Set Outobj = New Outlook.Application
With Tabelle1
For i = 2 To .Cells(Rows.Count, 1).End(xlUp).Row
oDic(.Cells(i, 1).Value) = 0
Next
tmp = oDic.keys
ReDim arrAdrZ(1 To oDic.Count, 1 To 3)
ReDim arrAdr(1 To oDic.Count, 1 To 5)
For i = LBound(tmp) To UBound(tmp)
If i < UBound(tmp) Then
arrAdrZ(i + 1, 1) = tmp(i)
arrAdrZ(i + 1, 2) = Application.Match(tmp(i), .Columns(1), 0)
arrAdrZ(i + 1, 3) = Application.Match(tmp(i + 1), .Columns(1), 0) - 1
Else
arrAdrZ(i + 1, 1) = tmp(i)
arrAdrZ(i + 1, 2) = Application.Match(tmp(i), .Columns(1), 0)
arrAdrZ(i + 1, 3) = .Cells(Rows.Count, 1).End(xlUp).Row
End If
Next i
For i = 1 To UBound(arrAdr)
With Outobj.CreateItem(0)
.GetInspector.Display ' öffnet den erzeugten Item
.To = arrAdrZ(i, 1)
.CC = Tabelle1.Cells(arrAdrZ(i, 2), 2).Value
.Subject = Tabelle1.Cells(arrAdrZ(i, 2), 4)
.htmlBody = "Hallo! " & Tabelle1.Cells(arrAdrZ(i, 2), 3) & "
" & Tabelle1.Cells(arrAdrZ(i, 2), 5) & "
" & _
TableHTMLtoBody(Tabelle1.Range("F1:H1"), Tabelle1.Range("F" & arrAdrZ(i, 2) & ":H" & arrAdrZ(i, 3))) & "
Mit freundlichen Grüßen
Dein Name
"
'.Send
End With
Next i
End With
Set Outobj = Nothing
End Sub
Function TableHTMLtoBody(xlRangeK As Range, xlRange As Range) As String
Dim i&, j&, htmlBody$, Zelle$
htmlBody = ""
' Überschriften (ggf. anpassen)
For i = 1 To xlRange.Columns.Count
htmlBody = htmlBody & "" & xlRangeK.Cells(1, i).Value & " "
Next i
' Datenzellen
htmlBody = htmlBody & ""
For i = 1 To xlRange.Rows.Count
htmlBody = htmlBody & ""
For j = 1 To xlRange.Columns.Count
Zelle = xlRange.Cells(i, j).Value
If IsEmpty(Zelle) Then
Zelle = " "
End If
htmlBody = htmlBody & "" & Zelle & " "
Next j
htmlBody = htmlBody & " "
Next i
htmlBody = htmlBody & "
"
TableHTMLtoBody = htmlBody
End Function
Option Explicit
' Beispiel mit Early Binding
' Verweis Microsoft Outlook 16.0 Library muss aktiviert sein (im diesem Beispiel bereit aktiviert)
Sub MailVersenden()
Dim Outobj As Outlook.Application
Dim Mail As Object, oDic As Object
Dim i&, j&, k&, tmp, arrAdrZ(), arrAdr()
Set oDic = CreateObject("Scripting.Dictionary")
Set Outobj = New Outlook.Application
With Tabelle1
For i = 2 To .Cells(Rows.Count, 1).End(xlUp).Row
oDic(.Cells(i, 1).Value) = 0
Next
tmp = oDic.keys
ReDim arrAdrZ(1 To oDic.Count, 1 To 3)
ReDim arrAdr(1 To oDic.Count, 1 To 5)
For i = LBound(tmp) To UBound(tmp)
If i < UBound(tmp) Then
arrAdrZ(i + 1, 1) = tmp(i)
arrAdrZ(i + 1, 2) = Application.Match(tmp(i), .Columns(1), 0)
arrAdrZ(i + 1, 3) = Application.Match(tmp(i + 1), .Columns(1), 0) - 1
Else
arrAdrZ(i + 1, 1) = tmp(i)
arrAdrZ(i + 1, 2) = Application.Match(tmp(i), .Columns(1), 0)
arrAdrZ(i + 1, 3) = .Cells(Rows.Count, 1).End(xlUp).Row
End If
Next i
For i = 1 To UBound(arrAdr)
With Outobj.CreateItem(0)
.GetInspector.Display ' öffnet den erzeugten Item
.To = arrAdrZ(i, 1)
.CC = Tabelle1.Cells(arrAdrZ(i, 2), 2).Value
.Subject = Tabelle1.Cells(arrAdrZ(i, 2), 4)
.htmlBody = "<html>Hallo " & Tabelle1.Cells(arrAdrZ(i, 2), 3) & ",<br><br>" & Tabelle1.Cells(arrAdrZ(i, 2), 5) & "<br><br>" & _
TableHTMLtoBody(Tabelle1.Range("F1:H1"), Tabelle1.Range("F" & arrAdrZ(i, 2) & ":H" & arrAdrZ(i, 3))) & "<br><br>Mit freundlichen Grüßen<br>Dein Name<br><br></html>"
'.Send
End With
Next i
End With
Set Outobj = Nothing
End Sub
Function TableHTMLtoBody(xlRangeK As Range, xlRange As Range) As String
Dim i&, j&, htmlBody$, Zelle$
htmlBody = "<TABLE border=0>"
' Überschriften (ggf. anpassen)
For i = 1 To xlRange.Columns.Count
htmlBody = htmlBody & "<TH bgcolor=a8a8a8>" & xlRangeK.Cells(1, i).Value & "</TH>"
Next i
' Datenzellen
htmlBody = htmlBody & "</TR>"
For i = 1 To xlRange.Rows.Count
htmlBody = htmlBody & "<TR>"
For j = 1 To xlRange.Columns.Count
Zelle = xlRange.Cells(i, j).Value
If IsEmpty(Zelle) Then
Zelle = " "
End If
htmlBody = htmlBody & "<TD>" & Zelle & "</TD>"
Next j
htmlBody = htmlBody & "</TR>"
Next i
htmlBody = htmlBody & "</TABLE>"
TableHTMLtoBody = htmlBody
End Function