31.05.2017, 10:57
(Dieser Beitrag wurde zuletzt bearbeitet: 31.05.2017, 13:03 von WillWissen.
Bearbeitungsgrund: Code in Codetags gesetzt
)
Moin, folgendes Dilemma:
Ich habe eine Tabelle in Excel wie folgt:
A client
B E-mail
C Fix fee
d fix fee 2
Ich wuerde gerne automatisch eine Mail an jeden Clienten verschicken und an bestimmten Stelllen in der E-Mail die Fix Fee und Fix Fee 2 erwaehnen.
Ausserdem wuerde ich gerne automatisch ein Dear {Client name} haben.
Bis jetzt habe ich weitesgehend Macro aus dem Internet benutzt, der diese E-mails erstellt aber ohne persoenliche Anrede und mit den Daten als Tabelle anstelle als Text.
Kann mir wer helfen entweder den Clientenname und die Mail aus der Tabelle zu entfernen oder aber die Daten als Text in der Mail zu posten?
Ich poste hier mal meinen jetzigen Macro:
Ich habe eine Tabelle in Excel wie folgt:
A client
B E-mail
C Fix fee
d fix fee 2
Ich wuerde gerne automatisch eine Mail an jeden Clienten verschicken und an bestimmten Stelllen in der E-Mail die Fix Fee und Fix Fee 2 erwaehnen.
Ausserdem wuerde ich gerne automatisch ein Dear {Client name} haben.
Bis jetzt habe ich weitesgehend Macro aus dem Internet benutzt, der diese E-mails erstellt aber ohne persoenliche Anrede und mit den Daten als Tabelle anstelle als Text.
Kann mir wer helfen entweder den Clientenname und die Mail aus der Tabelle zu entfernen oder aber die Daten als Text in der Mail zu posten?
Ich poste hier mal meinen jetzigen Macro:
Code:
Sub Send_Row_Or_Rows_2()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Don't forget to copy the function RangetoHTML in the module.
'Working in Excel 2000-2016
Dim OutApp As Object
Dim OutMail As Object
Dim rng As Range
Dim Ash As Worksheet
Dim Cws As Worksheet
Dim Rcount As Long
Dim Rnum As Long
Dim FilterRange As Range
Dim FieldNum As Integer
Dim StrBody As String
Dim SigString As String
Dim Signature As String
On Error GoTo cleanup
Set OutApp = CreateObject("Outlook.Application")
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
'Set filter sheet, you can also use Sheets("MySheet")
Set Ash = ActiveSheet
'Set filter range and filter column (column with e-mail addresses)
Set FilterRange = Ash.Range("A1:H" & Ash.rows.Count)
FieldNum = 2 'Filter column = B because the filter range start in column A
'Add a worksheet for the unique list and copy the unique list in A1
Set Cws = Worksheets.Add
FilterRange.Columns(FieldNum).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=Cws.Range("A1"), _
CriteriaRange:="", Unique:=True
'Count of the unique values + the header cell
Rcount = Application.WorksheetFunction.CountA(Cws.Columns(1))
StrBody = "<p style=""font-family:arial;"">Dear</p>" & "<br><br>" & _
"Mehr text"
'If there are unique values start the loop
If Rcount >= 2 Then
For Rnum = 2 To Rcount
'Filter the FilterRange on the FieldNum column
FilterRange.AutoFilter Field:=FieldNum, _
Criteria1:=Cws.Cells(Rnum, 1).Value
'If the unique value is a mail addres create a mail
If Cws.Cells(Rnum, 1).Value Like "?*@?*.?*" Then
With Ash.AutoFilter.Range
On Error Resume Next
Set rng = .SpecialCells(xlCellTypeVisible)
On Error GoTo 0
End With
SigString = Environ("appdata") & _
"\Microsoft\Signatures\Dario.htm"
If Dir(SigString) <> "" Then
Signature = GetBoiler(SigString)
Else
Signature = ""
End If
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = Cws.Cells(Rnum, 1).Value
.Subject =
.HTMLBody = StrBody & RangetoHTML(rng) & Signature
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
End If
Ash.AutoFilterMode = False
Next Rnum
End If
cleanup:
Set OutApp = Nothing
Application.DisplayAlerts = False
Cws.Delete
Application.DisplayAlerts = True
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
Function GetBoiler(ByVal sFile As String) As String
Dim fso As Object
Dim ts As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)
GetBoiler = ts.readall
ts.Close
End Function
Function GetSignature(fPath As String) As String
Dim fso As Object
Dim TSet As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set TSet = fso.GetFile(fPath).OpenAsTextStream(1, -2)
GetSignature = TSet.readall
TSet.Close
End Function
Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook
TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
'Copy the range and create a new workbook to past the data in
rng = Range("D:E")
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With
'Publish the sheet to a htm file
With TempWB.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=TempWB.Sheets(1).name, _
Source:=TempWB.Sheets(1).UsedRange.address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
'Read all data from the htm file into RangetoHTML
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.readall
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
"align=left x:publishsource=")
'Close TempWB
TempWB.Close savechanges:=False
'Delete the htm file we used in this function
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function