Jump to content

Error with _Word_DocFindReplace


Recommended Posts

1 hour ago, Weisgarnix said:

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

You still can use a mail template as long as you set the same CID to the attachment. Details can be found here:

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

@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 :/

Edited by Weisgarnix
Link to comment
Share on other sites

Bei genauer Betrachtung schließen sich OFT template und inline Bild aus, denn zuerst muss das Bild als Attachment (2) an die Mail (1) gehängt werden bevor sich der MailBody (3) auf das Bild beziehen kann. Leider werden die Schritte (1) und (3) eausgeführt, wenn die Mail mit dem OFT template erstellt wird.
Derzeit habe ich also leider keine Lösung für Dein Problem.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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!! 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 :blink:)

@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 ;)

Link to comment
Share on other sites

Great that you have a working solution now! I'm looking forward for your future questions ;)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

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...