Option Explicit On
'============< variables >============
Public Const °const_Password As String = "CodeDocu"
Public Const °Setup_Columns As String = "F:I"
Public Const °Input_Sheetname As String = "Checkliste"
Public Const °column_Check As String = "Ja/Nein"
Public Const °column_Senden As String = "Senden"
Public Const °column_Email As String = "EMail"
Public Const °column_CC As String = "CC"
Public Const °column_Anhang As String = "Anhang_Dokument"
Public Const °column_Vorlage As String = "Vorlage"
Public Const °Replaces As String = "Mitarbeitername;ID;Kreditlimit"
Public Const °CheckString1 As String = "Ja"
Public Const °CheckString2 As String = "x"
'============</ variables >============
'============< Menu >============
Public Sub Menu_Emails_senden()
Check_Email_Loop()
End Sub
'============< Emails >============
Public Sub Check_Email_Loop()
'-------------< Check_Email_Loop() >-------------
'< find_Range_To_Check >
Dim range_Check As Range
Set range_Check = sys_Get_Check_Range(°column_Senden)
'</ find_Range_To_Check >
'----< @Loop: Range_to_Check >----
Dim varCell As Range
For Each varCell In range_Check.Cells
If varCell.Value = °CheckString1 Then
'< get_Column_Send >
Dim sSenden As String
sSenden = sys_Get_RowValueOf(varCell.Row, °column_Senden)
'< get_Column_Send >
If sSenden = °CheckString2 Then
'< Create_Email >
Create_Email_by_RowNr varCell.Row
'</ Create_Email >
End If
End If
Next
'----</ @Loop: Range_to_Check >----
'< Abschluss >
MsgBox "Emails erstellt", vbInformation, "Fertig"
'</ Abschluss >
'-------------</ Check_Email_Loop() >-------------
End Sub
Private Sub Create_Email_by_RowNr(ByVal intRow As Integer)
'-------------< Create_Email_by_RowNr() >-------------
'< Werte >
Dim sAddress_To As String
sAddress_To = sys_Get_RowValueOf(intRow, °column_Email)
'< check >
If sAddress_To Like "" Then
MsgBox "Email Ziel-Adresse fehlt in Zeile " & intRow, vbCritical, "Setup in Liste unvollständig"
Exit Sub
End If
'</ check >
Dim sAddresses_CC As String
sAddresses_CC = sys_Get_RowValueOf(intRow, °column_CC)
Dim sAttachement As String
sAttachement = sys_Get_RowValueOf(intRow, °column_Anhang)
'</ Werte >
'--< TemplateSheet_Values >--
'< TemplateSheet >
Dim sTemplateSheet As String
sTemplateSheet = sys_Get_RowValueOf(intRow, °column_Vorlage)
'< check >
If sTemplateSheet Like "" Then
MsgBox "_Vorlage nicht eingetragen in Zeile " & intRow, vbCritical, "Setup in Liste unvollständig"
Exit Sub
End If
'</ check >
Dim wsTemplateSheet As Worksheet
Set wsTemplateSheet = ActiveWorkbook.Worksheets(sTemplateSheet)
'</ TemplateSheet >
Dim sTitle As String
sTitle = wsTemplateSheet.Range("B2").Value
'< Text_Template >
Dim varText_Template As Shape
Set varText_Template = wsTemplateSheet.Shapes(1)
Dim sText As String
sText = varText_Template.TextFrame2.TextRange.Text
'</ Text_Template >
'--</ TemplateSheet_Values >--
Dim arrReplaces
arrReplaces = Split(°Replaces, ";")
Dim varWord As Variant
For Each varWord In arrReplaces
If Not IsNull(varWord) Then
Dim sWord As String
sWord = CStr(varWord)
If Not sWord Like "" Then
Dim sPlaceholder As String
sPlaceholder = "[@" & sWord & "]"
Dim sReplace
sReplace = ActiveWorkbook.Names("var" & sWord).RefersToRange.Text
sText = Replace(sText, sPlaceholder, sReplace, , , vbTextCompare)
sTitle = Replace(sTitle, sPlaceholder, sReplace, , , vbTextCompare)
End If
End If
Next
'--< Send Email >--
Send_Email sTitle, sText, sAddress_To, sAddresses_CC, sAttachement
'--</ Send Email >--
'-------------< Create_Email_by_RowNr() >-------------
End Sub
Public Sub Send_Email(ByVal sTitle As String, ByVal sText As String, ByVal sAddress_To As String, ByVal sAddresses_CC As String, ByVal sAttachements As String)
'-------------< Send_Email() >-------------
'< init >
On Error Resume Next
'< outlook >
Dim app_Outlook As Object
Set app_Outlook = CreateObject("Outlook.Application")
'</ outlook >
'--< Send Email >--
Dim objEmail As Object
Set objEmail = app_Outlook.CreateItem(0)
objEmail.To = sAddress_To
If Not sAddresses_CC Like "" Then
objEmail.CC = sAddresses_CC
' Dim arrAddresses() As String
' arrAddresses = Split(sAddresses_CC, ";")
' Dim Address_CC
' For Each Address_CC In arrAddresses
' objEmail.CC.Add Address_CC
' Next
End If
objEmail.Subject = sTitle
objEmail.Body = sText '*.body for Text, Richtext
'objEmail.HTMLBody = sHTML '*.HTMLBody for HTML
If Not sAttachements Like "" Then
If Not sAttachements Like "*:*" Then
Dim sBasePath As String
sBasePath = ActiveWorkbook.Names("varPfad_Email_Attachement").RefersToRange.Text
If Not sBasePath Like "*\" Then
sBasePath = sBasePath & "\"
End If
sAttachements = sBasePath & sAttachements
End If
objEmail.Attachments.Add sAttachements
End If
'< send >
Dim sAutosend As String
sAutosend = ActiveWorkbook.Names("varEmail_Autosend").RefersToRange.Text
If sAutosend = "Ja" Then
objEmail.Display False
objEmail.Send
Else
objEmail.Display False
'objEmail.Display bVisible '*no visible=true because of : wait on outlook
End If
'</ send >
'--</ create Email >--
'< Abschluss >
Set objEmail = Nothing
Set app_Outlook = Nothing
'</ Abschluss >
If Err.Number <> 0 Then
MsgBox Err.Description & vbCrLf & "Eventuell Email-Adresse anpassen", vbCritical, "Error on sending.."
End If
'-------------</ Send_Email() >-------------
End Sub
'============</ Emails >============
|