Jump to content

Weisgarnix

Active Members
  • Posts

    23
  • Joined

  • Last visited

Weisgarnix's Achievements

Seeker

Seeker (1/7)

0

Reputation

  1. YES!! It now works also with the graphics file inline AND the pdf named correctly in the attachment! Thank you very much! I once planned to build up a gui but I think that I'm not familiar enough for coding a mass creating personalized mails including personalized attachments Nevertheless, I'm now able to use it! Thank you, @Subz and @water! If I decide to build up a gui, I will let you know (latest when I fail again ) @water: sounds logically. I also tried out *.msg for that matter but that actually did not help. For the moment, the workaround with the reg-key works
  2. @Subz this is the code: #cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.14.2 Author: myName Script Function: Template AutoIt script. #ce ---------------------------------------------------------------------------- ; Script Start - Add your code below here #include <Excel.au3> #include <File.au3> #include <Array.au3> #include <IE.au3> #include <MsgBoxConstants.au3> #include <Word.au3> #include <AutoItConstants.au3> #include "Source\OutlookEX.au3" Global $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc") Global $sTMP_FilePath = @TempDir&"\PMail" If FileExists($sTMP_FilePath) Then DirRemove($sTMP_FilePath & "\") Global $sSRC_XLS_FilePath = @ScriptDir&"\liste.xlsx" Global $sSRC_DOC_FilePath = @ScriptDir&"\Einladung_Test.docx" Global $sTMP_PDF_FilePath = $sTMP_FilePath & "\Einladung_Test.pdf" Global $sSRC_OFT_FilePath = @ScriptDir&"\Einladung_Test.oft" Global $sTMP_OFT_FilePath = $sTMP_FilePath & "\Einladung_Test.msg" Global $aUserInfo = _pmailexcelreadfile($sSRC_XLS_FilePath) For $i = 1 To UBound($aUserInfo) - 1 FileCopy($sSRC_OFT_FilePath, $sTMP_OFT_FilePath, 9) _pmailwordreplacetext($sSRC_DOC_FilePath, $sTMP_PDF_FilePath, $i) Sleep(2000) Local $aAttachments[1]=[$sTMP_PDF_FilePath] _pmailoutlookpreparemail($aUserInfo[$i][0], "Exklusive Einladung zum Meet'n Greet für %$%Vorname%$% %$%NACHNAME%$%", $sTMP_OFT_FilePath, $aAttachments, "some-mailadress@googlemail.com", $i) Sleep(2000) ; _pmailoutlooksendmail(@TempDir&"\PMail\Einladung_Test.oft") Sleep(2000) DirRemove($sTMP_FilePath, $DIR_REMOVE) Next Func _pmailexcelreadfile($sWorkbook, $vRange = Default) Local $oExcel =_Excel_Open(False) Local $oWorkbook = _Excel_BookOpen($oExcel, $sWorkbook, True) Local $aWorkbook = _Excel_RangeRead($oWorkbook, Default, $vRange) _Excel_BookClose($oWorkbook) _Excel_Close($oExcel, False, True) If Not IsArray($aWorkbook) Then Exit MsgBox(16,"Error!","Excelfile could not be read to an array. Please check your excelfile and your range!") Return $aWorkbook EndFunc Func _pmailwordreplacetext($sDoc, $sPDFDoc, $iUserInfo = 1) Local $oWord = _Word_Create(False) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _pmailwordreplacetext", "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended) Local $oDoc = _Word_DocOpen($oWord, $sDoc, False, Default, True) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _pmailwordreplacetext", "Error opening '"& $sDoc & "'." & @CRLF & "@error = " & @error & ", @extended = " & @extended) For $oStoryRange In $oDoc.StoryRanges _Word_DocFindReplace($oDoc, $aUserInfo[0][1], $aUserInfo[$iUserInfo][1], Default, $oStoryRange) _Word_DocFindReplace($oDoc, $aUserInfo[0][2], $aUserInfo[$iUserInfo][2], Default, $oStoryRange) While 1 If IsObj($oStoryRange.NextStoryRange) Then $oStoryRange = $oStoryRange.NextStoryRange _Word_DocFindReplace($oDoc, $aUserInfo[0][1], $aUserInfo[$iUserInfo][1], Default, $oStoryRange) _Word_DocFindReplace($oDoc, $aUserInfo[0][2], $aUserInfo[$iUserInfo][2], Default, $oStoryRange) Else ExitLoop EndIf WEnd Next _Word_DocSaveAs($oDoc, $sPDFDoc, $wdFormatPDF) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _pmailwordreplacetext", "Error saving the Word document." & @CRLF & "@error = " & @error & ", @extended = " & @extended) _Word_DocClose($oDoc) _Word_Quit($oWord) EndFunc Func _pmailoutlookpreparemail($recipientadress, $subject, $oftfilepath, $attachmentsarray, $bcc="", $iUserInfo = 1) Local $oOutlook = _OL_Open() Local $oItem = _OL_ItemCreate($oOutlook, $olMailItem, "", $oftfilepath) If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemCreate Example Script", "Error creating the mail in folder. @error = " & @error & ", @extended = " & @extended) $oItem.HTMLBody = StringReplace($oItem.HTMLBody, $aUserInfo[0][1], $aUserInfo[$iUserInfo][1]) Local $aItem = _OL_ItemGet($oOutlook, $oItem, Default, "Subject") If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemGet", "Error accessing current item. @error = " & @error & ", @extended = " & @extended) $aItem[1][1] = StringReplace(StringReplace($aItem[1][1], $aUserInfo[0][1], $aUserInfo[$iUserInfo][1]), $aUserInfo[0][2], $aUserInfo[$iUserInfo][2]) _OL_ItemModify($oOutlook, $oItem, Default, "Subject=" & $aItem[1][1]) If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemModify", "Error accessing current item. @error = " & @error & ", @extended = " & @extended) $oItem1 = _OL_ItemRecipientAdd($oOutlook, $oItem, Default, $olTo, $recipientadress) If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemRecipientAdd", "Error adding recipient. @error = " & @error & ", @extended = " & @extended) If $bcc <> "" Then $oItem2 = _OL_ItemRecipientAdd($oOutlook, $oItem, Default, $olBCC, $bcc) If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemRecipientAdd", "Error adding bbc recipient. @error = " & @error & ", @extended = " & @extended) EndIf If(UBound($attachmentsarray)>0) Then _OL_ItemAttachmentAdd($oOutlook, $oItem, Default, $attachmentsarray) If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemAttachmentAdd ", "Error adding attachment'. @error = " & @error & ", @extended = " & @extended) EndIf ;_OL_ItemSave($oOutlook, $oItem, Default, $oftfilepath, $olMSG, 3) _OL_ItemSend($oOutlook, $oItem) _OL_Close($oOutlook) EndFunc Func _pmailoutlooksendmail($oftfilepath) $oOutlook = _OL_Open() $oItem = _OL_ItemCreate($oOutlook, $olMailItem, Default, $oftfilepath) If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemCreate Example Script", "Error creating a mail in folder 'Outlook-UDF-Test\TargetFolder\Mail'. @error = " & @error & ", @extended = " & @extended) _OL_ItemSend($oOutlook, $oItem) _OL_Close($oOutlook) EndFunc Func _ErrFunc($oError) ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _ @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _ @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _ @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _ @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _ @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _ @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _ @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _ @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _ @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF) EndFunc ;==>_ErrFunc So, its the code you sent me. Should be the same, dont know why its working different on our two systems. I have Win7 x64bit and Office2013 32bit installed. @water I'm to stupid for understanding how to find out the CID of the image in the oft-Template and mark it as inline and hidden attachment In German we say "I only understand railwaystation" which means that I know the single words you're saying but I have no Idea what I shall do now :/
  3. Hi Subz, no, it looks similar to "before", when I saved the mail and sent it out manually:
  4. So the idea I had once to have the mail completely prepared as an oft file will not work, I will need to create the mail without the possibility to use an oft file if I want to use pictures in the mail body?
  5. Hi. Allthough I dont know what was wrong with my code, I'm happy to have your working code @Subz thanks a lot!! It looks pretty good! Just preparing the mails is even better since the sending can still be done using outlook manually meaning the sender is still doing that manually. I just have one problem left: The picture is not directly included in the mailtext but only attached and the pdf will be renamed to a file called "noname" (without extension) in the mail Has anybody an idea what to do to fix that behaviour? Thanks a lot to both of you!!
  6. Found a mistake: Need to call _pmailoutlookpreparemail($adressatenliste[$j+1], "Exklusive Einladung zum Meet'n Greet für %$%Vorname%$% %$%NACHNAME%$%", $aGrid, @TempDir&"\PMail\Einladung_Test.oft", $attacharray, "some-mailadress@googlemail.com") instead of _pmailoutlookpreparemail($adressatenliste[$j], "Exklusive Einladung zum Meet'n Greet für %$%Vorname%$% %$%NACHNAME%$%", $aGrid, @TempDir&"\PMail\Einladung_Test.oft", $attacharray, "some-mailadress@googlemail.com") but that actually didn't change anything about the error
  7. I will create a version without any empty lines and without any comments. Give me a minute...^^ Edit: I dont understand that! #include <Excel.au3> #include <File.au3> #include <Array.au3> #include <IE.au3> #include <MsgBoxConstants.au3> #include <Word.au3> #include <AutoItConstants.au3> #include "Source\OutlookEX.au3" Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc") $adressatenliste = _pmailexcelreadfile(@ScriptDir&"\liste.xlsx", "A1:A10") $searchandreplacearray = _pmailexcelreadfile(@ScriptDir&"\liste.xlsx", "B1:C10") $j=0 While $j <= 8 Local $aGrid[2][2] $aGrid[0][0] = $searchandreplacearray[0][0] $aGrid[0][1] = $searchandreplacearray[$j+1][0] $aGrid[1][0] = $searchandreplacearray[0][1] $aGrid[1][1] = $searchandreplacearray[$j+1][1] FileCopy(@ScriptDir&"\Einladung_Test.docx", @TempDir&"\PMail\Einladung_Test.docx", $FC_OVERWRITE + $FC_CREATEPATH) FileCopy(@ScriptDir&"\Einladung_Test.oft", @TempDir&"\PMail\Einladung_Test.oft", 9) Sleep(2000) _pmailwordreplacetext(@TempDir&"\PMail\Einladung_Test.docx", $aGrid) Sleep(2000) _pmailwordsaveaspdf(@TempDir&"\PMail\Einladung_Test.docx", @TempDir&"\PMail\Einladung_Test.pdf") Local $attacharray[1]=[@TempDir&"\PMail\Einladung_Test.pdf"] Sleep(2000) _pmailoutlookpreparemail($adressatenliste[$j], "Exklusive Einladung zum Meet'n Greet für %$%Vorname%$% %$%NACHNAME%$%", $aGrid, @TempDir&"\PMail\Einladung_Test.oft", $attacharray, "some-mailadress@googlemail.com") Sleep(2000) _pmailoutlooksendmail(@TempDir&"\PMail\Einladung_Test.oft") Sleep(2000) DirRemove(@TempDir&"\PMail\", $DIR_REMOVE) WEnd Func _pmailexcelreadfile($excelfilepath, $range) Local $oExcel =_Excel_Open() $data = _Excel_BookOpen($oExcel,$excelfilepath) $extracted = _Excel_RangeRead($data, Default, $range) _Excel_BookClose($data) ProcessClose("Excel.exe") If Not IsArray($extracted) Then MsgBox(16,"Error!","Excelfile could not be read to an array. Please check your excelfile and your range!") Else Return $extracted EndIf EndFunc Func _pmailwordreplacetext($wordfilepath, $snrarray) Local $oWord = _Word_Create(False) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _pmailwordreplacetext", _ "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended) Local $oDoc = _Word_DocOpen($oWord, $wordfilepath) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _pmailwordreplacetext", _ "Error opening '"&$wordfilepath&"'." & @CRLF & "@error = " & @error & ", @extended = " & @extended) For $i = 0 To UBound($snrarray) - 1 For $oStoryRange In $oDoc.StoryRanges _Word_DocFindReplace($oDoc, $snrarray[$i][0], $snrarray[$i][1], Default, $oStoryRange) While 1 If IsObj($oStoryRange.NextStoryRange) Then $oStoryRange = $oStoryRange.NextStoryRange _Word_DocFindReplace($oDoc, $snrarray[$i][0], $snrarray[$i][1], Default, $oStoryRange) Else ExitLoop EndIf WEnd Next Next _Word_DocSave($oDoc) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _pmailwordreplacetext", _ "Error saving the Word document." & @CRLF & "@error = " & @error & ", @extended = " & @extended) _Word_DocClose($oDoc) _Word_Quit($oWord) EndFunc Func _pmailwordsaveaspdf($wordfilepath, $pdffilepath) Local $oWord = _Word_Create(False) Local $oDoc = _Word_DocOpen($oWord, $wordfilepath) _Word_DocSaveAs($oDoc, $pdffilepath, $wdFormatPDF) _Word_DocClose($oDoc) _Word_Quit($oWord) EndFunc Func _pmailoutlookpreparemail($recipientadress, $subject, $snrarray, $oftfilepath, $attachmentsarray, $bcc="") $snrarraysize=UBound($snrarray) $i=0 While($i < $snrarraysize) _ReplaceStringInFile($oftfilepath, $snrarray[$i][0], $snrarray[$i][1]) $subject = StringReplace($subject, $snrarray[$i][0], $snrarray[$i][1], 0, $STR_CASESENSE) $i+=1 WEnd $oOL = _OL_Open() $oItem = _OL_ItemCreate($oOL, $olMailItem, Default, $oftfilepath) If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemCreate Example Script", "Error creating the mail in folder. @error = " & @error & ", @extended = " & @extended) MsgBox(0, "", "Anfang") $oInspector = $oOL.ActiveInspector If @error Then MsgBox(0, "Error", "ActiveInspector returned @error = " & @error) Exit EndIf MsgBox(0, "", "A") $oItem0 = $oInspector.CurrentItem MsgBox(0, "", "B") $aProperties = _OL_ItemGet($oOL, $oItem0, Default, "Subject") MsgBox(0, "", "C") If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemGet", "Error accessing current item. @error = " & @error & ", @extended = " & @extended) $sSubject = $aProperties[1][1] & " - Changed Subject" MsgBox(0, "", "D") _OL_ItemModify($oOL, $oItem, Default, "Subject=" & $sSubject) MsgBox(0, "", "E") If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemModify", "Error accessing current item. @error = " & @error & ", @extended = " & @extended) $oItem1 = _OL_ItemRecipientAdd($oOL, $oItem, Default, $olTo, $recipientadress) MsgBox(0, "", "F") If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemRecipientAdd", "Error adding recipient. @error = " & @error & ", @extended = " & @extended) If $bcc <> "" Then MsgBox(0, "", "G") $oItem2 = _OL_ItemRecipientAdd($oOL, $oItem, Default, $olBCC, $bcc) MsgBox(0, "", "H") If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemRecipientAdd", "Error adding bbc recipient. @error = " & @error & ", @extended = " & @extended) EndIf MsgBox(0, "", "I") If(UBound($attachmentsarray)>0) Then _OL_ItemAttachmentAdd($oOL, $oItem, Default, $attachmentsarray) If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemAttachmentAdd ", "Error adding attachment'. @error = " & @error & ", @extended = " & @extended) MsgBox(0, "", "J") EndIf _OL_ItemSave($oOL, $oItem, Default, $oftfilepath, $olTemplate, 3) MsgBox(0, "", "K") EndFunc Func _pmailoutlooksendmail($oftfilepath) $oOL = _OL_Open() $oItem = _OL_ItemCreate($oOL, $olMailItem, Default, $oftfilepath) If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemCreate Example Script", "Error creating a mail in folder 'Outlook-UDF-Test\TargetFolder\Mail'. @error = " & @error & ", @extended = " & @extended) _OL_ItemSend($oOL, $oItem) _OL_Close($oOL) EndFunc Func _ErrFunc($oError) ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _ @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _ @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _ @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _ @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _ @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _ @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _ @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _ @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _ @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF) EndFunc ;==>_ErrFunc I have no explaination for this. Does he include the lines from the used funktions from the includes in counting?
  8. Yes, AutoIt saves the word document as a pdf. This works, the pdf is generated and the script continues.
  9. Sure, thank you again at this point! I would be lost without your help!! #cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.14.1 Author: Armin Wiesmüller Script Function: providing functions for mass creating and sending personalized mails #ce ---------------------------------------------------------------------------- #include <Excel.au3> #include <File.au3> #include <Array.au3> #include <IE.au3> #include <MsgBoxConstants.au3> #include <Word.au3> #include <AutoItConstants.au3> #include "Source\OutlookEX.au3" ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;Const $wdFormatPDF = 17 ; Register the default Word.au3 COM Error Handler ;_WordErrorHandlerRegister() ; ############################################################################### ; ### T # E # S # T # - # A # R # E # A ### 5 # 1 ##### A # N # F # A # N # G ### ; ############################################################################### ; Überlegungen ; Anzahl Spalten = Anzahl Search and Replaces + 1 ; Anzahl Zeilen = Anzahl Adressaten + 1 ; Error monitoring. This will trap all COM errors while alive. Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc") $adressatenliste = _pmailexcelreadfile(@ScriptDir&"\liste.xlsx", "A1:A10") $searchandreplacearray = _pmailexcelreadfile(@ScriptDir&"\liste.xlsx", "B1:C10") $j=0 While $j <= 8 ;Local $aGrid[][] = [[$searchandreplacearray[0][0], [$searchandreplacearray[0][1]]], [$searchandreplacearray[$j+1][0], $searchandreplacearray[$j+1][1]]] Local $aGrid[2][2] $aGrid[0][0] = $searchandreplacearray[0][0] $aGrid[0][1] = $searchandreplacearray[$j+1][0] $aGrid[1][0] = $searchandreplacearray[0][1] $aGrid[1][1] = $searchandreplacearray[$j+1][1] FileCopy(@ScriptDir&"\Einladung_Test.docx", @TempDir&"\PMail\Einladung_Test.docx", $FC_OVERWRITE + $FC_CREATEPATH) FileCopy(@ScriptDir&"\Einladung_Test.oft", @TempDir&"\PMail\Einladung_Test.oft", 9) Sleep(2000) _pmailwordreplacetext(@TempDir&"\PMail\Einladung_Test.docx", $aGrid) Sleep(2000) _pmailwordsaveaspdf(@TempDir&"\PMail\Einladung_Test.docx", @TempDir&"\PMail\Einladung_Test.pdf") Local $attacharray[1]=[@TempDir&"\PMail\Einladung_Test.pdf"] Sleep(2000) _pmailoutlookpreparemail($adressatenliste[$j], "Exklusive Einladung zum Meet'n Greet für %$%Vorname%$% %$%NACHNAME%$%", $aGrid, @TempDir&"\PMail\Einladung_Test.oft", $attacharray, "some-mailadress@googlemail.com") Sleep(2000) _pmailoutlooksendmail(@TempDir&"\PMail\Einladung_Test.oft") Sleep(2000) DirRemove(@TempDir&"\PMail\", $DIR_REMOVE) WEnd ; ############################################################################### ; ### # T # E # S # T # - # A # R # E # A ### 5 # 1 ########### E # N # D # E ### ; ############################################################################### ; ############################################################################### ; ### USER DEFINED FUNCTIONS - DO NOT CHANGE IF NOT SURE WHAT YOU'RE DOING!!! ### ; ############################################################################### ; #FUNCTION# ==================================================================================================================== ; Name ..........: _pmailexcelreadfile ; Description ...: Reads the content of a defined range from an excelfile to an array ; Syntax ........: _readexcelfile($excelfilepath, $range) ; Parameters ....: $excelfilepath - string: path to the excelfile. Will not be checked in this udf for existance! ; $range - string: a range in the form "A1:B10". ; Return values .: $adressates - array of strings: including the information read from the cells in the range of the excelfile. ; Author ........: Armin Wiesmüller ; Modified ......: 02.02.2017 ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _pmailexcelreadfile($excelfilepath, $range) Local $oExcel =_Excel_Open() $data = _Excel_BookOpen($oExcel,$excelfilepath) $extracted = _Excel_RangeRead($data, Default, $range) _Excel_BookClose($data) ProcessClose("Excel.exe") If Not IsArray($extracted) Then MsgBox(16,"Error!","Excelfile could not be read to an array. Please check your excelfile and your range!") Else Return $extracted EndIf EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _pmailwordreplacetext ; Description ...: ; Syntax ........: _pmailwordreplace($wordfilepath, $snrarray) ; Parameters ....: $wordfilepath - string: path to the wordfile that will be modified. ; $snrarray - array of strings: containing search ([x][0]) and corresponding replace [x][1] patterns. ; Return values .: None (works on the given file) ; Author ........: Armin Wiesmüller ; Modified ......: 02.02.2017 ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _pmailwordreplacetext($wordfilepath, $snrarray) ; Create application object Local $oWord = _Word_Create(False) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _pmailwordreplacetext", _ "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended) ; Open document Local $oDoc = _Word_DocOpen($oWord, $wordfilepath) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _pmailwordreplacetext", _ "Error opening '"&$wordfilepath&"'." & @CRLF & "@error = " & @error & ", @extended = " & @extended) ; Replace operation For $i = 0 To UBound($snrarray) - 1 For $oStoryRange In $oDoc.StoryRanges _Word_DocFindReplace($oDoc, $snrarray[$i][0], $snrarray[$i][1], Default, $oStoryRange) While 1 If IsObj($oStoryRange.NextStoryRange) Then $oStoryRange = $oStoryRange.NextStoryRange _Word_DocFindReplace($oDoc, $snrarray[$i][0], $snrarray[$i][1], Default, $oStoryRange) Else ExitLoop EndIf WEnd Next Next ; Save document _Word_DocSave($oDoc) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _pmailwordreplacetext", _ "Error saving the Word document." & @CRLF & "@error = " & @error & ", @extended = " & @extended) _Word_DocClose($oDoc) _Word_Quit($oWord) EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _pmailwordsaveaspdf ; Description ...: ; Syntax ........: _pmailwordsaveaspdf($wordfilepath, $pdffilepath) ; Parameters ....: $wordfilepath - string: path and filename of the word-document to be converted. ; $pdffilepath - string: path and filename of the destination file. ; Return values .: None (will create a new file in given destination) ; Author ........: Armin Wiesmüller ; Modified ......: 02.02.2017 ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _pmailwordsaveaspdf($wordfilepath, $pdffilepath) ; Create application object Local $oWord = _Word_Create(False) ;If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", _ ; "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended) ; Open document Local $oDoc = _Word_DocOpen($oWord, $wordfilepath) ;If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", _ ; "Error opening '"&$wordfilepath&"'." & @CRLF & "@error = " & @error & ", @extended = " & @extended) _Word_DocSaveAs($oDoc, $pdffilepath, $wdFormatPDF) _Word_DocClose($oDoc) _Word_Quit($oWord) EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _pmailoutlookpreparemail ; Description ...: ; Syntax ........: _pmailoutlookpreparemail($recipientadress, $subject, $snrarray, $oftfilepath, ; $attachmentsarray[, $bcc = ""]) ; Parameters ....: $recipientadress - string: mail adress of the recipient - noch checked for containing an @ and at least one dot. ; $subject - string: containing the subject including replacemant patterns. ; $snrarray - array of strings: containing search ([x][0]) and corresponding replace [x][1] patterns. ; $oftfilepath - string: path to the template file to fill up. ; $attachmentsarray - array of strings: containing the paths to the files to be attached. ; $bcc - [optional] string: bcc-adress to send the mails to - e.g. for archive reason. Default is "". ; Return values .: None ; Author ........: Armin Wiesmüller ; Modified ......: 02.02.2017 ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _pmailoutlookpreparemail($recipientadress, $subject, $snrarray, $oftfilepath, $attachmentsarray, $bcc="") ; Create a Copy of the file in the destination location ; OLD CODE - ADD BEFORE CALLING THIS FUNCTION: FileCopy($oftsourcefilepath, $oftdestinationfilepath, $FC_OVERWRITE + $FC_CREATEPATH) $snrarraysize=UBound($snrarray) $i=0 While($i < $snrarraysize) _ReplaceStringInFile($oftfilepath, $snrarray[$i][0], $snrarray[$i][1]) $subject = StringReplace($subject, $snrarray[$i][0], $snrarray[$i][1], 0, $STR_CASESENSE) $i+=1 WEnd $oOL = _OL_Open() $oItem = _OL_ItemCreate($oOL, $olMailItem, Default, $oftfilepath) If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemCreate Example Script", "Error creating the mail in folder. @error = " & @error & ", @extended = " & @extended) MsgBox(0, "", "Anfang") $oInspector = $oOL.ActiveInspector If @error Then MsgBox(0, "Error", "ActiveInspector returned @error = " & @error) Exit EndIf MsgBox(0, "", "A") $oItem0 = $oInspector.CurrentItem MsgBox(0, "", "B") ; Get Subject property $aProperties = _OL_ItemGet($oOL, $oItem0, Default, "Subject") MsgBox(0, "", "C") If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemGet", "Error accessing current item. @error = " & @error & ", @extended = " & @extended) ; Modify Subject property $sSubject = $aProperties[1][1] & " - Changed Subject" MsgBox(0, "", "D") _OL_ItemModify($oOL, $oItem, Default, "Subject=" & $sSubject) MsgBox(0, "", "E") If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemModify", "Error accessing current item. @error = " & @error & ", @extended = " & @extended) ; add recipient and probably also bcc $oItem1 = _OL_ItemRecipientAdd($oOL, $oItem, Default, $olTo, $recipientadress) MsgBox(0, "", "F") If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemRecipientAdd", "Error adding recipient. @error = " & @error & ", @extended = " & @extended) If $bcc <> "" Then MsgBox(0, "", "G") $oItem2 = _OL_ItemRecipientAdd($oOL, $oItem, Default, $olBCC, $bcc) MsgBox(0, "", "H") If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemRecipientAdd", "Error adding bbc recipient. @error = " & @error & ", @extended = " & @extended) EndIf MsgBox(0, "", "I") If(UBound($attachmentsarray)>0) Then _OL_ItemAttachmentAdd($oOL, $oItem, Default, $attachmentsarray) If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemAttachmentAdd ", "Error adding attachment'. @error = " & @error & ", @extended = " & @extended) MsgBox(0, "", "J") EndIf _OL_ItemSave($oOL, $oItem, Default, $oftfilepath, $olTemplate, 3) MsgBox(0, "", "K") EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _pmailoutlooksendmail ; Description ...: ; Syntax ........: _pmailoutlooksendmail($oftfilepath) ; Parameters ....: $oftfilepath - string: path to the oft file to be sent. ; Return values .: None ; Author ........: Armin Wiesmüller ; Modified ......: 02.02.2017 ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _pmailoutlooksendmail($oftfilepath) $oOL = _OL_Open() $oItem = _OL_ItemCreate($oOL, $olMailItem, Default, $oftfilepath) If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemCreate Example Script", "Error creating a mail in folder 'Outlook-UDF-Test\TargetFolder\Mail'. @error = " & @error & ", @extended = " & @extended) ; Send the mail _OL_ItemSend($oOL, $oItem) ; Close Outlook connection _OL_Close($oOL) EndFunc Func _ErrFunc($oError) ; Do anything here. ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _ @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _ @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _ @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _ @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _ @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _ @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _ @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _ @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _ @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF) EndFunc ;==>_ErrFunc
  10. Uh. Now I will get nightmares this night!!
  11. Changed it to your code snippet but I get the same error: "D:\CloudStation\Programmierung\AutoIt\Personalisierte-E-Mail\logic.au3" (207) : ==> The requested action with this object has failed.: $oItem0 = $oInspector.CurrentItem $oItem0 = $oInspector^ ERROR #cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.14.1 Author: Armin Wiesmüller Script Function: providing functions for mass creating and sending personalized mails #ce ---------------------------------------------------------------------------- #include <Excel.au3> #include <File.au3> #include <Array.au3> #include <IE.au3> #include <MsgBoxConstants.au3> #include <Word.au3> #include <AutoItConstants.au3> #include "Source\OutlookEX.au3" ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;Const $wdFormatPDF = 17 ; Register the default Word.au3 COM Error Handler ;_WordErrorHandlerRegister() ; ############################################################################### ; ### T # E # S # T # - # A # R # E # A ### 5 # 1 ##### A # N # F # A # N # G ### ; ############################################################################### ; Überlegungen ; Anzahl Spalten = Anzahl Search and Replaces + 1 ; Anzahl Zeilen = Anzahl Adressaten + 1 $adressatenliste = _pmailexcelreadfile(@ScriptDir&"\liste.xlsx", "A1:A10") $searchandreplacearray = _pmailexcelreadfile(@ScriptDir&"\liste.xlsx", "B1:C10") $j=0 While $j <= 8 ;Local $aGrid[][] = [[$searchandreplacearray[0][0], [$searchandreplacearray[0][1]]], [$searchandreplacearray[$j+1][0], $searchandreplacearray[$j+1][1]]] Local $aGrid[2][2] $aGrid[0][0] = $searchandreplacearray[0][0] $aGrid[0][1] = $searchandreplacearray[$j+1][0] $aGrid[1][0] = $searchandreplacearray[0][1] $aGrid[1][1] = $searchandreplacearray[$j+1][1] FileCopy(@ScriptDir&"\Einladung_Test.docx", @TempDir&"\PMail\Einladung_Test.docx", $FC_OVERWRITE + $FC_CREATEPATH) FileCopy(@ScriptDir&"\Einladung_Test.oft", @TempDir&"\PMail\Einladung_Test.oft", 9) Sleep(2000) _pmailwordreplacetext(@TempDir&"\PMail\Einladung_Test.docx", $aGrid) Sleep(2000) _pmailwordsaveaspdf(@TempDir&"\PMail\Einladung_Test.docx", @TempDir&"\PMail\Einladung_Test.pdf") Local $attacharray[1]=[@TempDir&"\PMail\Einladung_Test.pdf"] Sleep(2000) _pmailoutlookpreparemail($adressatenliste[$j], "Exklusive Einladung zum Meet'n Greet für %$%Vorname%$% %$%NACHNAME%$%", $aGrid, @TempDir&"\PMail\Einladung_Test.oft", $attacharray, "some-mailadress@googlemail.com") Sleep(2000) _pmailoutlooksendmail(@TempDir&"\PMail\Einladung_Test.oft") Sleep(2000) DirRemove(@TempDir&"\PMail\", $DIR_REMOVE) WEnd ; ############################################################################### ; ### # T # E # S # T # - # A # R # E # A ### 5 # 1 ########### E # N # D # E ### ; ############################################################################### ; ############################################################################### ; ### USER DEFINED FUNCTIONS - DO NOT CHANGE IF NOT SURE WHAT YOU'RE DOING!!! ### ; ############################################################################### ; #FUNCTION# ==================================================================================================================== ; Name ..........: _pmailexcelreadfile ; Description ...: Reads the content of a defined range from an excelfile to an array ; Syntax ........: _readexcelfile($excelfilepath, $range) ; Parameters ....: $excelfilepath - string: path to the excelfile. Will not be checked in this udf for existance! ; $range - string: a range in the form "A1:B10". ; Return values .: $adressates - array of strings: including the information read from the cells in the range of the excelfile. ; Author ........: Armin Wiesmüller ; Modified ......: 02.02.2017 ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _pmailexcelreadfile($excelfilepath, $range) Local $oExcel =_Excel_Open() $data = _Excel_BookOpen($oExcel,$excelfilepath) $extracted = _Excel_RangeRead($data, Default, $range) _Excel_BookClose($data) ProcessClose("Excel.exe") If Not IsArray($extracted) Then MsgBox(16,"Error!","Excelfile could not be read to an array. Please check your excelfile and your range!") Else Return $extracted EndIf EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _pmailwordreplacetext ; Description ...: ; Syntax ........: _pmailwordreplace($wordfilepath, $snrarray) ; Parameters ....: $wordfilepath - string: path to the wordfile that will be modified. ; $snrarray - array of strings: containing search ([x][0]) and corresponding replace [x][1] patterns. ; Return values .: None (works on the given file) ; Author ........: Armin Wiesmüller ; Modified ......: 02.02.2017 ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _pmailwordreplacetext($wordfilepath, $snrarray) ; Create application object Local $oWord = _Word_Create(False) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _pmailwordreplacetext", _ "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended) ; Open document Local $oDoc = _Word_DocOpen($oWord, $wordfilepath) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _pmailwordreplacetext", _ "Error opening '"&$wordfilepath&"'." & @CRLF & "@error = " & @error & ", @extended = " & @extended) ; Replace operation For $i = 0 To UBound($snrarray) - 1 For $oStoryRange In $oDoc.StoryRanges _Word_DocFindReplace($oDoc, $snrarray[$i][0], $snrarray[$i][1], Default, $oStoryRange) While 1 If IsObj($oStoryRange.NextStoryRange) Then $oStoryRange = $oStoryRange.NextStoryRange _Word_DocFindReplace($oDoc, $snrarray[$i][0], $snrarray[$i][1], Default, $oStoryRange) Else ExitLoop EndIf WEnd Next Next ; Save document _Word_DocSave($oDoc) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _pmailwordreplacetext", _ "Error saving the Word document." & @CRLF & "@error = " & @error & ", @extended = " & @extended) _Word_DocClose($oDoc) _Word_Quit($oWord) EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _pmailwordsaveaspdf ; Description ...: ; Syntax ........: _pmailwordsaveaspdf($wordfilepath, $pdffilepath) ; Parameters ....: $wordfilepath - string: path and filename of the word-document to be converted. ; $pdffilepath - string: path and filename of the destination file. ; Return values .: None (will create a new file in given destination) ; Author ........: Armin Wiesmüller ; Modified ......: 02.02.2017 ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _pmailwordsaveaspdf($wordfilepath, $pdffilepath) ; Create application object Local $oWord = _Word_Create(False) ;If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", _ ; "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended) ; Open document Local $oDoc = _Word_DocOpen($oWord, $wordfilepath) ;If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", _ ; "Error opening '"&$wordfilepath&"'." & @CRLF & "@error = " & @error & ", @extended = " & @extended) _Word_DocSaveAs($oDoc, $pdffilepath, $wdFormatPDF) _Word_DocClose($oDoc) _Word_Quit($oWord) EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _pmailoutlookpreparemail ; Description ...: ; Syntax ........: _pmailoutlookpreparemail($recipientadress, $subject, $snrarray, $oftfilepath, ; $attachmentsarray[, $bcc = ""]) ; Parameters ....: $recipientadress - string: mail adress of the recipient - noch checked for containing an @ and at least one dot. ; $subject - string: containing the subject including replacemant patterns. ; $snrarray - array of strings: containing search ([x][0]) and corresponding replace [x][1] patterns. ; $oftfilepath - string: path to the template file to fill up. ; $attachmentsarray - array of strings: containing the paths to the files to be attached. ; $bcc - [optional] string: bcc-adress to send the mails to - e.g. for archive reason. Default is "". ; Return values .: None ; Author ........: Armin Wiesmüller ; Modified ......: 02.02.2017 ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _pmailoutlookpreparemail($recipientadress, $subject, $snrarray, $oftfilepath, $attachmentsarray, $bcc="") ; Create a Copy of the file in the destination location ; OLD CODE - ADD BEFORE CALLING THIS FUNCTION: FileCopy($oftsourcefilepath, $oftdestinationfilepath, $FC_OVERWRITE + $FC_CREATEPATH) $snrarraysize=UBound($snrarray) $i=0 While($i < $snrarraysize) _ReplaceStringInFile($oftfilepath, $snrarray[$i][0], $snrarray[$i][1]) $subject = StringReplace($subject, $snrarray[$i][0], $snrarray[$i][1], 0, $STR_CASESENSE) $i+=1 WEnd $oOL = _OL_Open() $oItem = _OL_ItemCreate($oOL, $olMailItem, Default, $oftfilepath) If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemCreate Example Script", "Error creating the mail in folder. @error = " & @error & ", @extended = " & @extended) MsgBox(0, "", "Anfang") $oInspector = $oOL.ActiveInspector If @error Then MsgBox(0, "Error", "ActiveInspector returned @error = " & @error) Exit EndIf MsgBox(0, "", "A") $oItem0 = $oInspector.CurrentItem MsgBox(0, "", "B") ; Get Subject property $aProperties = _OL_ItemGet($oOL, $oItem0, Default, "Subject") MsgBox(0, "", "C") If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemGet", "Error accessing current item. @error = " & @error & ", @extended = " & @extended) ; Modify Subject property $sSubject = $aProperties[1][1] & " - Changed Subject" MsgBox(0, "", "D") _OL_ItemModify($oOL, $oItem, Default, "Subject=" & $sSubject) MsgBox(0, "", "E") If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemModify", "Error accessing current item. @error = " & @error & ", @extended = " & @extended) ; add recipient and probably also bcc $oItem1 = _OL_ItemRecipientAdd($oOL, $oItem, Default, $olTo, $recipientadress) MsgBox(0, "", "F") If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemRecipientAdd", "Error adding recipient. @error = " & @error & ", @extended = " & @extended) If $bcc <> "" Then MsgBox(0, "", "G") $oItem2 = _OL_ItemRecipientAdd($oOL, $oItem, Default, $olBCC, $bcc) MsgBox(0, "", "H") If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemRecipientAdd", "Error adding bbc recipient. @error = " & @error & ", @extended = " & @extended) EndIf MsgBox(0, "", "I") If(UBound($attachmentsarray)>0) Then _OL_ItemAttachmentAdd($oOL, $oItem, Default, $attachmentsarray) If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemAttachmentAdd ", "Error adding attachment'. @error = " & @error & ", @extended = " & @extended) MsgBox(0, "", "J") EndIf _OL_ItemSave($oOL, $oItem, Default, $oftfilepath, $olTemplate, 3) MsgBox(0, "", "K") EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _pmailoutlooksendmail ; Description ...: ; Syntax ........: _pmailoutlooksendmail($oftfilepath) ; Parameters ....: $oftfilepath - string: path to the oft file to be sent. ; Return values .: None ; Author ........: Armin Wiesmüller ; Modified ......: 02.02.2017 ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _pmailoutlooksendmail($oftfilepath) $oOL = _OL_Open() $oItem = _OL_ItemCreate($oOL, $olMailItem, Default, $oftfilepath) If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemCreate Example Script", "Error creating a mail in folder 'Outlook-UDF-Test\TargetFolder\Mail'. @error = " & @error & ", @extended = " & @extended) ; Send the mail _OL_ItemSend($oOL, $oItem) ; Close Outlook connection _OL_Close($oOL) EndFunc I dont think that I will get it working until Easter. Next year. I'm too bad at programming
  12. Yes, using the latest version helped me with this error. Next one. I want to cry out loud. I'm to stupid for programming "D:\CloudStation\Programmierung\AutoIt\Personalisierte-E-Mail\logic.au3" (251) : ==> The requested action with this object has failed.: $oItem0 = $oOL.ActiveInspector.CurrentItem $oItem0 = $oOL.ActiveInspector^ ERROR How do I get more information about what the error exactly is?
  13. Hi water, thanks, fixed it there as well, but the error is already happening at the first call
  14. hi Subz. Thank you for the help, Dont know why I've overseen that. I now added the Default but unfortunately still get the same error: --------------------------- OutlookEX UDF: _OL_ItemCreate Example Script --------------------------- Error creating the mail in folder. @error = 1, @extended = 4 --------------------------- OK --------------------------- Does somebody have an idea? Here is the current source code: #cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.14.1 Author: Armin Wiesmüller Script Function: providing functions for mass creating and sending personalized mails #ce ---------------------------------------------------------------------------- #include <Excel.au3> #include <File.au3> #include <Array.au3> #include <IE.au3> #include <MsgBoxConstants.au3> #include <Word.au3> #include <AutoItConstants.au3> #include "Source\OutlookEX.au3" ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;Const $wdFormatPDF = 17 ; Register the default Word.au3 COM Error Handler ;_WordErrorHandlerRegister() ; ############################################################################### ; ### T # E # S # T # - # A # R # E # A ### 5 # 1 ##### A # N # F # A # N # G ### ; ############################################################################### ; Überlegungen ; Anzahl Spalten = Anzahl Search and Replaces + 1 ; Anzahl Zeilen = Anzahl Adressaten + 1 $adressatenliste = _pmailexcelreadfile(@ScriptDir&"\liste.xlsx", "A1:A10") $searchandreplacearray = _pmailexcelreadfile(@ScriptDir&"\liste.xlsx", "B1:C10") ;_ArrayDisplay($adressatenliste) ;_ArrayDisplay($searchandreplacearray) $j=0 While $j <= 8 ;Local $aGrid[][] = [[$searchandreplacearray[0][0], [$searchandreplacearray[0][1]]], [$searchandreplacearray[$j+1][0], $searchandreplacearray[$j+1][1]]] Local $aGrid[2][2] $aGrid[0][0] = $searchandreplacearray[0][0] $aGrid[0][1] = $searchandreplacearray[$j+1][0] $aGrid[1][0] = $searchandreplacearray[0][1] $aGrid[1][1] = $searchandreplacearray[$j+1][1] FileCopy(@ScriptDir&"\Einladung_Test.docx", @TempDir&"\PMail\Einladung_Test.docx", $FC_OVERWRITE + $FC_CREATEPATH) FileCopy(@ScriptDir&"\Einladung_Test.oft", @TempDir&"\PMail\Einladung_Test.oft", 9) Sleep(2000) _ArrayDisplay($aGrid) _pmailwordreplacetext(@TempDir&"\PMail\Einladung_Test.docx", $aGrid) Sleep(2000) _pmailwordsaveaspdf(@TempDir&"\PMail\Einladung_Test.docx", @TempDir&"\PMail\Einladung_Test.pdf") Local $attacharray[1]=[@TempDir&"\PMail\Einladung_Test.pdf"] Sleep(2000) ; _pmailoutlookpreparemail($recipientadress, $subject, $snrarray, $oftfilepath, $attachmentsarray, $bcc="") _pmailoutlookpreparemail($adressatenliste[$j], "Exklusive Einladung zum Meet'n Greet für %$%Vorname%$% %$%NACHNAME%$%", $aGrid, @TempDir&"\PMail\Einladung_Test.oft", $attacharray, "some-mailadress@googlemail.com") Sleep(2000) _pmailoutlooksendmail(@TempDir&"\PMail\Einladung_Test.oft") Sleep(2000) DirRemove(@TempDir&"\PMail\", $DIR_REMOVE) WEnd ; ############################################################################### ; ### # T # E # S # T # - # A # R # E # A ### 5 # 1 ########### E # N # D # E ### ; ############################################################################### ; ############################################################################### ; ### USER DEFINED FUNCTIONS - DO NOT CHANGE IF NOT SURE WHAT YOU'RE DOING!!! ### ; ############################################################################### ; #FUNCTION# ==================================================================================================================== ; Name ..........: _pmailexcelreadfile ; Description ...: Reads the content of a defined range from an excelfile to an array ; Syntax ........: _readexcelfile($excelfilepath, $range) ; Parameters ....: $excelfilepath - string: path to the excelfile. Will not be checked in this udf for existance! ; $range - string: a range in the form "A1:B10". ; Return values .: $adressates - array of strings: including the information read from the cells in the range of the excelfile. ; Author ........: Armin Wiesmüller ; Modified ......: 02.02.2017 ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _pmailexcelreadfile($excelfilepath, $range) Local $oExcel =_Excel_Open() $data = _Excel_BookOpen($oExcel,$excelfilepath) $extracted = _Excel_RangeRead($data, Default, $range) _Excel_BookClose($data) ProcessClose("Excel.exe") ;If IsArray($adressates) Then _ArrayDisplay($adressates) If Not IsArray($extracted) Then MsgBox(16,"Error!","Excelfile could not be read to an array. Please check your excelfile and your range!") Else Return $extracted EndIf EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _pmailwordreplacetext ; Description ...: ; Syntax ........: _pmailwordreplace($wordfilepath, $snrarray) ; Parameters ....: $wordfilepath - string: path to the wordfile that will be modified. ; $snrarray - array of strings: containing search ([x][0]) and corresponding replace [x][1] patterns. ; Return values .: None (works on the given file) ; Author ........: Armin Wiesmüller ; Modified ......: 02.02.2017 ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _pmailwordreplacetext($wordfilepath, $snrarray) ; Create application object Local $oWord = _Word_Create(False) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _pmailwordreplacetext", _ "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended) ; Open document Local $oDoc = _Word_DocOpen($oWord, $wordfilepath) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _pmailwordreplacetext", _ "Error opening '"&$wordfilepath&"'." & @CRLF & "@error = " & @error & ", @extended = " & @extended) ; ***************************************************************************** ; Replace operation ; ***************************************************************************** ;DEBUG!!! ;_ArrayDisplay($snrarray) For $i = 0 To UBound($snrarray) - 1 For $oStoryRange In $oDoc.StoryRanges _Word_DocFindReplace($oDoc, $snrarray[$i][0], $snrarray[$i][1], Default, $oStoryRange) While 1 If IsObj($oStoryRange.NextStoryRange) Then $oStoryRange = $oStoryRange.NextStoryRange _Word_DocFindReplace($oDoc, $snrarray[$i][0], $snrarray[$i][1], Default, $oStoryRange) Else ExitLoop EndIf WEnd Next Next ;MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", "Paragraph control character successfully replaced.") ; Save document _Word_DocSave($oDoc) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _pmailwordreplacetext", _ "Error saving the Word document." & @CRLF & "@error = " & @error & ", @extended = " & @extended) ;MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocSave Example", "Document successfully saved as '" & _ ; $oDoc.FullName & "'") _Word_DocClose($oDoc) _Word_Quit($oWord) EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _pmailwordsaveaspdf ; Description ...: ; Syntax ........: _pmailwordsaveaspdf($wordfilepath, $pdffilepath) ; Parameters ....: $wordfilepath - string: path and filename of the word-document to be converted. ; $pdffilepath - string: path and filename of the destination file. ; Return values .: None (will create a new file in given destination) ; Author ........: Armin Wiesmüller ; Modified ......: 02.02.2017 ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _pmailwordsaveaspdf($wordfilepath, $pdffilepath) ; Create application object Local $oWord = _Word_Create(False) ;If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", _ ; "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended) ; Open document Local $oDoc = _Word_DocOpen($oWord, $wordfilepath) ;If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", _ ; "Error opening '"&$wordfilepath&"'." & @CRLF & "@error = " & @error & ", @extended = " & @extended) _Word_DocSaveAs($oDoc, $pdffilepath, $wdFormatPDF) #CS If @error And $WordComErrorNumber = -2147352567 Then ; MsgBox(0, "Error", 'You first need to install the "2007 Microsoft Office Add-in: Microsoft Save as PDF".' & @CRLF & @CRLF & _ ; "The download link will now be opened for you.") ; _IECreate("http://www.microsoft.com/downloads/details.aspx?FamilyId=F1FC413C-6D89-4F15-991B-63B07BA5F2E5&displaylang=en", 0, 1, 0) ; EndIf #CE _Word_DocClose($oDoc) _Word_Quit($oWord) EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _pmailoutlookpreparemail ; Description ...: ; Syntax ........: _pmailoutlookpreparemail($recipientadress, $subject, $snrarray, $oftfilepath, ; $attachmentsarray[, $bcc = ""]) ; Parameters ....: $recipientadress - string: mail adress of the recipient - noch checked for containing an @ and at least one dot. ; $subject - string: containing the subject including replacemant patterns. ; $snrarray - array of strings: containing search ([x][0]) and corresponding replace [x][1] patterns. ; $oftfilepath - string: path to the template file to fill up. ; $attachmentsarray - array of strings: containing the paths to the files to be attached. ; $bcc - [optional] string: bcc-adress to send the mails to - e.g. for archive reason. Default is "". ; Return values .: None ; Author ........: Armin Wiesmüller ; Modified ......: 02.02.2017 ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _pmailoutlookpreparemail($recipientadress, $subject, $snrarray, $oftfilepath, $attachmentsarray, $bcc="") ; Create a Copy of the file in the destination location ; OLD CODE - ADD BEFORE CALLING THIS FUNCTION: FileCopy($oftsourcefilepath, $oftdestinationfilepath, $FC_OVERWRITE + $FC_CREATEPATH) $snrarraysize=UBound($snrarray) $i=0 While($i < $snrarraysize) _ReplaceStringInFile($oftfilepath, $snrarray[$i][0], $snrarray[$i][1]) $subject = StringReplace($subject, $snrarray[$i][0], $snrarray[$i][1], 0, $STR_CASESENSE) $i+=1 WEnd $oOL = _OL_Open() $oItem = _OL_ItemCreate($oOL, $olMailItem, Default, $oftfilepath) If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemCreate Example Script", "Error creating the mail in folder. @error = " & @error & ", @extended = " & @extended) ; Get current item $oItem0 = $oOL.ActiveInspector.CurrentItem If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: $oOL.ActiveInspector.CurrentItem", "Error accessing current Inspector. @error = " & @error & ", @extended = " & @extended) ; Get Subject property $aProperties = _OL_ItemGet($oOL, $oItem0, Default, "Subject") If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemGet", "Error accessing current item. @error = " & @error & ", @extended = " & @extended) ; Modify Subject property $sSubject = $aProperties[1][1] & " - Changed Subject" _OL_ItemModify($oOL, $oItem, Default, "Subject=" & $sSubject) If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemModify", "Error accessing current item. @error = " & @error & ", @extended = " & @extended) ; add recipient and probably also bcc $oItem1 = _OL_ItemRecipientAdd($oOL, $oItem, Default, $olTo, $recipientadress) If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemRecipientAdd", "Error adding recipient. @error = " & @error & ", @extended = " & @extended) If $bcc <> "" Then $oItem2 = _OL_ItemRecipientAdd($oOL, $oItem, Default, $olBCC, $bcc) If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemRecipientAdd", "Error adding bbc recipient. @error = " & @error & ", @extended = " & @extended) EndIf If(UBound($attachmentsarray)>0) Then _OL_ItemAttachmentAdd($oOL, $oItem, Default, $attachmentsarray) If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemAttachmentAdd ", "Error adding attachment'. @error = " & @error & ", @extended = " & @extended) EndIf _OL_ItemSave($oOL, $oItem, Default, $oftfilepath, $olTemplate, 3) EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _pmailoutlooksendmail ; Description ...: ; Syntax ........: _pmailoutlooksendmail($oftfilepath) ; Parameters ....: $oftfilepath - string: path to the oft file to be sent. ; Return values .: None ; Author ........: Armin Wiesmüller ; Modified ......: 02.02.2017 ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _pmailoutlooksendmail($oftfilepath) $oOL = _OL_Open() $oItem = _OL_ItemCreate($oOL, $olMailItem, $oftfilepath) ;If @error <> 0 Then Exit MsgBox(16, "OutlookEX UDF: _OL_ItemCreate Example Script", "Error creating a mail in folder 'Outlook-UDF-Test\TargetFolder\Mail'. @error = " & @error & ", @extended = " & @extended) ; Send the mail _OL_ItemSend($oOL, $oItem) ; Close Outlook connection _OL_Close($oOL) EndFunc
  15. Okay, I will be able to live with the fact that it cannot replace strings within text boxes. But I now have an other problem with the function _OL_ItemCreate. The error code says that the folder could not be accessed. The extended code says that the folder does not exist. But I dont know which folder they mean?
×
×
  • Create New...