Sign in to follow this  
Followers 0
KEHT

Merge multiple Word documents created from template into one

4 posts in this topic

Hello guys!!!

I am a total newbie in AutoIT.  It's literally my second day writing code in it and I am trying to finish my first application.  One last piece of the puzzle is proving hard to crack. I googled the Internet, but could not find any straight forward answer.  I have a MS Word (.dot) 1-page template file that I open and populate using values from a dictionary.  That part is working fine, but what I need at the end is a single Word file where this 1-pagers are combined into one multi-page doc.

I have a hunch that "_Word_DocRangeSet" function needs to be used, but I am just unable to grasp how to copy and paste data from one opened doc into another.  Here's what I have so far in a function.  Again, please forgive me for messy code (it's a rough draft).    Any and all help is appreciated.

Func createWordDoc($recordHash)
    $oWordApp = _Word_Create(0, True)
    $oFinalWordApp = _Word_Create(0, True)
    $oDoc_2 = _Word_DocAdd($oFinalWordApp)

    For $myHR In $recordHash
        $oDoc = _Word_DocAdd($oWordApp, $wdNewBlankDocument, @ScriptDir &"\CASTemplate.dot")
        _Word_DocFindReplace($oDoc, "<ByText>", _ObjDictGetValue($recordHash, $myHR))
        _Word_DocFindReplace($oDoc, "<BillNo>", $myHR)

        With $oWordApp
            .content.Select
            .ActiveWindow.Selection.Copy
        EndWith

        $oRange = _Word_DocRangeSet($oDoc_2, -2)
        $oRange.InsertAfter($oFinalWordApp.ActiveWindow.Selection.Paste)
        $oRange.InsertBreak($wdPageBreak)

    Next

    $oFinalWordApp.Visible = true
    _Word_DocSaveAs($oDoc_2, @ScriptDir & "\_Word_Test2.doc")
    _Word_DocClose($oDoc_2)
    Return $oDoc_2
EndFunc

Share this post


Link to post
Share on other sites



Unfortunately there is no _Word_DocCopyPaste function available (at the moment).

Use _Word_DocRangeSet to mark the range you want to copy. Then use the Range.Copy method to copy the marked range to the clipboard.

Use _Word_DocRangeSet to mark a range of 0 length at the end of the target document and use the Range.Paste method to copy the content of the clipboard.

1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Unfortunately there is no _Word_DocCopyPaste function available (at the moment).

Use _Word_DocRangeSet to mark the range you want to copy. Then use the Range.Copy method to copy the marked range to the clipboard.

Use _Word_DocRangeSet to mark a range of 0 length at the end of the target document and use the Range.Paste method to copy the content of the clipboard.

Thanks a lot for your guidance!  It put me on the right path.  I ended up using Range.PasteAndFormat(16) because it didn't quite look the same in the final document.

Share this post


Link to post
Share on other sites

:)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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
Sign in to follow this  
Followers 0