Jump to content

Error with _Word_DocFindReplace


Recommended Posts

I assume you do not use the latest version of the OutlookEX UDF which does not support Default as parameter.
Use

$oItem = _OL_ItemCreate($oOL, $olMailItem, "", $oftfilepath)

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

If there is no active Inspector then the next operation fails.
Try this one:

$oInspector = $oOL.ActiveInspector
If @error Then 
    MsgBox(0, "Error", "ActiveInspector returned @error = " & @error)
    Exit
EndIf
$oItem0 = $oInspector.CurrentItem

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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 :(

Link to comment
Share on other sites

You need to add a COM error handler to grab the error. The following code will tell us exactly what goes wrong.

; Error monitoring. This will trap all COM errors while alive.
    Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")
    
    ... Your script goes here
    
    ; User's COM error function. Will be called if COM error occurs
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

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Uh. Now I will get nightmares this night!!

 

Quote

>"C:\Program Files (x86)\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "D:\CloudStation\Programmierung\AutoIt\Personalisierte-E-Mail\logic.au3"    
logic.au3 (65) : ==> COM Error intercepted !
    err.number is:         0x80020006
    err.windescription:    Unbekannter Name.

    err.description is:     
    err.source is:         
    err.helpfile is:     
    err.helpcontext is:     
    err.lastdllerror is:     0
    err.scriptline is:     65
    err.retcode is:     0x00000000

logic.au3 (65) : ==> COM Error intercepted !
    err.number is:         0x80020006
    err.windescription:    Unbekannter Name.

    err.description is:     
    err.source is:         
    err.helpfile is:     
    err.helpcontext is:     
    err.lastdllerror is:     0
    err.scriptline is:     65
    err.retcode is:     0x00000000

logic.au3 (53) : ==> COM Error intercepted !
    err.number is:         0x80020006
    err.windescription:    Unbekannter Name.

    err.description is:     
    err.source is:         
    err.helpfile is:     
    err.helpcontext is:     
    err.lastdllerror is:     0
    err.scriptline is:     53
    err.retcode is:     0x00000000

logic.au3 (53) : ==> COM Error intercepted !
    err.number is:         0x80020006
    err.windescription:    Unbekannter Name.

    err.description is:     
    err.source is:         
    err.helpfile is:     
    err.helpcontext is:     
    err.lastdllerror is:     0
    err.scriptline is:     53
    err.retcode is:     0x00000000

logic.au3 (188) : ==> COM Error intercepted !
    err.number is:         0x80020006
    err.windescription:    Unbekannter Name.

    err.description is:     
    err.source is:         
    err.helpfile is:     
    err.helpcontext is:     
    err.lastdllerror is:     0
    err.scriptline is:     188
    err.retcode is:     0x00000000

"D:\CloudStation\Programmierung\AutoIt\Personalisierte-E-Mail\logic.au3" (210) : ==> The requested action with this object has failed.:
$oItem0 = $oInspector.CurrentItem
$oItem0 = $oInspector^ ERROR

 

Link to comment
Share on other sites

Can you please post the current version of your script? Adding the COM error handler changed the line numbers.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

Strange :huh:
Line 53 is where you call

_pmailwordsaveaspdf(@TempDir&"\PMail\Einladung_Test.docx", @TempDir&"\PMail\Einladung_Test.pdf")

and the rest of the lines reported by the COM error handler are comment lines.
Do you have an idea what happens here?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I mean all lines reported by the COM error handler (e.g. logic.au3 (65) : ==> COM Error intercepted !) should show a line where COM objects are being processed.
So error messages and source code lines do not fit together.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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
Quote

>"C:\Program Files (x86)\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "D:\CloudStation\Programmierung\AutoIt\Personalisierte-E-Mail\logic_3.au3"    
logic_3.au3 (65) : ==> COM Error intercepted !
    err.number is:         0x80020006
    err.windescription:    Unbekannter Name.

    err.description is:     
    err.source is:         
    err.helpfile is:     
    err.helpcontext is:     
    err.lastdllerror is:     0
    err.scriptline is:     65
    err.retcode is:     0x00000000

logic_3.au3 (65) : ==> COM Error intercepted !
    err.number is:         0x80020006
    err.windescription:    Unbekannter Name.

    err.description is:     
    err.source is:         
    err.helpfile is:     
    err.helpcontext is:     
    err.lastdllerror is:     0
    err.scriptline is:     65
    err.retcode is:     0x00000000

logic_3.au3 (53) : ==> COM Error intercepted !
    err.number is:         0x80020006
    err.windescription:    Unbekannter Name.

    err.description is:     
    err.source is:         
    err.helpfile is:     
    err.helpcontext is:     
    err.lastdllerror is:     0
    err.scriptline is:     53
    err.retcode is:     0x00000000

logic_3.au3 (53) : ==> COM Error intercepted !
    err.number is:         0x80020006
    err.windescription:    Unbekannter Name.

    err.description is:     
    err.source is:         
    err.helpfile is:     
    err.helpcontext is:     
    err.lastdllerror is:     0
    err.scriptline is:     53
    err.retcode is:     0x00000000

logic_3.au3 (188) : ==> COM Error intercepted !
    err.number is:         0x80020006
    err.windescription:    Unbekannter Name.

    err.description is:     
    err.source is:         
    err.helpfile is:     
    err.helpcontext is:     
    err.lastdllerror is:     0
    err.scriptline is:     188
    err.retcode is:     0x00000000

"D:\CloudStation\Programmierung\AutoIt\Personalisierte-E-Mail\logic_3.au3" (96) : ==> The requested action with this object has failed.:
$oItem0 = $oInspector.CurrentItem
$oItem0 = $oInspector^ ERROR
>Exit code: 1    Time: 28.99

I have no explaination for this. Does he include the lines from the used funktions from the includes in counting?

Edited by Weisgarnix
Link to comment
Share on other sites

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 :(

Edited by Weisgarnix
Link to comment
Share on other sites

Can you try the following:

Note you will need to fix the variable names in Word doc as the doc you uploaded is different to the Excel spreadsheet and Oft file Variables i.e. 
%$%VORNAME%$%
%$%NACHNAME$%$

For testing I've commented out the _pmailoutlooksendmail, so this will create all the emails in your Drafts folder, this now correctly replaces the Oft body and subject text, and the Word text.

#include <Excel.au3>
#include <File.au3>
#include <Array.au3>
#include <IE.au3>
#include <MsgBoxConstants.au3>
#include <Word.au3>
#include <AutoItConstants.au3>
#include "sources\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.oft"

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, $olTemplate, 3)
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

 

Edited by Subz
Link to comment
Share on other sites

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!! :thumbsup:

Link to comment
Share on other sites

Sure! Please check example 4 in _OL_ItemCreate.au3 how to use CID.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

If you replace:

_OL_ItemSave($oOutlook, $oItem, Default, $oftfilepath, $olTemplate, 3)

with

_OL_ItemSend($oOutlook, $oItem)
_OL_Close($oOutlook)

and comment out _pmailoutlooksendmail($oftfilepath), whats your result, I can confirm that both Outlook and Gmail display the inline image and the PDF as an attachment correctly named.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...