#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.3.0
 Author:         Thomas Schwerdt

 Script Function:
	Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Include <Array.au3>
#include <GUILISTbox.au3>
#include <Clipboard.au3>
#include <Excel.au3>
#include <ProgressConstants.au3>

#Region ### START Koda GUI section ### Form=c:\dokumente und einstellungen\schwerdt\desktop\tom\autoit scripts\form1.kxf
$Form1_1 = GUICreate("Outlook Serienbrief", 543, 280, -1, -1)
$Label1 = GUICtrlCreateLabel("Vorlage wählen", 16, 20, 77, 17)
$iTemplate = GUICtrlCreateInput("", 104, 16, 281, 21)
$bTemplate = GUICtrlCreateButton("Durchsuchen", 400, 16, 121, 21, $WS_GROUP)
$Label2 = GUICtrlCreateLabel("Verteiler wählen", 16, 52, 79, 17)
$iMailList = GUICtrlCreateInput("", 104, 48, 281, 21)
$bMailList = GUICtrlCreateButton("Durchsuchen", 399, 50, 121, 21, $WS_GROUP)
$Label3 = GUICtrlCreateLabel("Betreff", 16, 84, 47, 17)
$iSubject = GUICtrlCreateInput("", 104, 80, 281, 21)
$Label4 = GUICtrlCreateLabel("Anhänge", 16, 116, 47, 17)
$lAttach = GUICtrlCreateList("", 103, 114, 283, 58,0)
$bAttachADD = GUICtrlCreateButton("Hinzufügen", 399, 116, 121, 21, $WS_GROUP)
$bAttachDel = GUICtrlCreateButton("Entfernen", 399, 148, 121, 21, $WS_GROUP)
$bSend = GUICtrlCreateButton("Versenden", 104, 187, 417, 21, $WS_GROUP)
$cProgressLabel=GUICtrlCreateLabel("Status: Bereit",104,228,453,16)
$cProgress = GUICtrlCreateProgress(104,246,417,16)
GUISetState(@SW_SHOW)

#EndRegion ### END Koda GUI section ###


Dim $arAttach[1]

;Functions
Func CreateMailItem()

EndFunc

While 1
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $bTemplate
			$bTFile = FileOpenDialog("Vorlage wählen",@ScriptDir,"Word- und Textdokumente (*.doc;*.docx;*.txt)",1)
			GUICtrlSetData($iTemplate,$bTFile)
		Case $bMailList
			$bMFile = FileOpenDialog("Verteiler wählen",@ScriptDir,"Exceldokumente (*.xls;*.xlsx)",1)
			GUICtrlSetData($iMailList,$bMFile)
		Case $bAttachADD
			$bAFile = FileOpenDialog("Anhang wählen",@ScriptDir,"Alle Dateien (*.*)",1)

			$ArAFN = StringSplit ( $bAFile, "\")
			$bAFileName = $ArAFN[$ArAFN[0]]

			If _ArraySearch($arAttach,$bAFileName,0,0,0,1) > 0 Then
				MsgBox(0,"","Datei schon vorhanden")
			Else
				GUICtrlSetData($lAttach,$bAFileName)
				_ArrayAdd($arAttach, $bAFile)
			EndIf
		Case $bAttachDel
			$lAttachData = GUICtrlRead($lAttach)
			If $lAttachData = "" Then
				MsgBox(0,"","Keine Datei ausgewählt")
			Else
				;delete from Array
				$delIndex = _ArraySearch($arAttach,$lAttachData,0,0,0,1)
				_ArrayDelete($arAttach,$delIndex)

				;Update list
				_GUICtrlListBox_DeleteString($lAttach,$delIndex-1)
			EndIf
		Case $bSend
			$templateFile = GUICtrlRead($iTemplate)
			$MailListFile = GUICtrlRead($iMailList)

			;Excel einlesen
			GUICtrlSetData($cProgressLabel, "Status: Verteiler wird erstellt")
			$oExcel = _ExcelBookOpen($MailListFile, 0)
			$zeilen = $oExcel.Worksheets(1).UsedRange.Rows.Count
			$aAnrede = _ExcelReadArray($oExcel, 2, 1, $zeilen, 1)
			$aEmail = _ExcelReadArray($oExcel, 2, 2, $zeilen, 1)
			_ExcelBookClose($oExcel , 0, 0)
			$CountEmails=UBound($aAnrede) - 1

			;Sicherheitsabfrage
			MsgBox(49,"Hinweis",$CountEmails & " Emails werden versendet!")

			For $i2 = 0 To $CountEmails - 1
				;Word - Ersetzen und kopieren
				$Word = ObjCreate("Word.Application")
				$Word.Documents.Open($templateFile)
				$Word.Visible = "False"

				$Word.Selection.Find.Execute("<<Anrede>>", 0, 0, 0, 0, 0, 1, 1, 0, $aAnrede[$i2], 2, 0, 0, 0, 0)
				 ;$Word.Selection.Find.Execute("<<Name>>", 0, 0, 0, 0, 0, 1, 1, 0, "Xxxxxxx Yyyyyyyy", 2, 0, 0, 0, 0)
				$Word.Selection.WholeStory
				$Word.Selection.Copy
				; Execute(FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, Replace, MatchKashida, MatchDiacritics, MatchAlefHamza, MatchControl)
				$Word.ActiveDocument.Close(0)
				$Word.quit()

				;CreateMailItem()
				Local $olFormatHTML = 2
				Local $olFormatPlain = 1
				Local $olFormatRichText = 3
				Local $olFormatUnspecified = 0
				Local $olMailItem = 0

				$oOutlook = ObjCreate("Outlook.Application")
				$oOMail = $oOutlook.CreateItem($olMailItem)

				With $oOMail
					.To = ($aEmail[$i2])
					.Subject = GUICtrlRead($iSubject)
					.BodyFormat = 2
					.Body = ""
					If UBound($arAttach) > 1 Then
						For $i = 1 to UBound($arAttach)-1
							.attachments.add ($arAttach[$i])
						Next
					EndIf
					.Display
					WinWaitActive(GUICtrlRead($iSubject))
					WinSetState(GUICtrlRead($iSubject),"",@SW_HIDE)
					Sleep(3000)
					Send("^v")
					;.Selection.WholeStory
					;.Selection.Insert
					.Send
				EndWith

				;MsgBox(0,"","Mail versendet an" & $aAnrede[$i2] & " - " & $aEmail[$i2])
				GUICtrlSetData($cProgress,100/$CountEmails*($i2+1))
				GUICtrlSetData($cProgressLabel, "Status: " & ($i2+1) & " von " & $CountEmails & " Emails versendet")
			Next
			Sleep(1000)

			GUICtrlSetData($cProgress,0)
			GUICtrlSetData($cProgressLabel, "Status: Bereit")

		Case $GUI_EVENT_CLOSE
			Exit

EndSwitch
WEnd
