Jump to content

Merge multiple Word documents created from template into one


Go to solution Solved by water,

Recommended Posts

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
Link to comment
Share on other sites

  • Solution

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.

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

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.

Link to comment
Share on other sites

:)

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

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