Jump to content

Merge word documents


MrCheese
 Share

Recommended Posts

Hi Guys,

Firstly, thanks for your  help in the past.

I have a new activity I need to accomplish.

In summary:

* need to read a cell in excel (containing a file name)

* open the file name in word (as its a word document

*copy the word document

*paste the word document into the master document

*read next cell in excel

... and repeat until you reach the bottom of the column.

 

I can read cells open workbooks etc.

But as far as copying and pasting in word - where is the best place to start, and what functions should I be looking at. Or even if autoit is the right system to use?

Thanks

 

 

 

Link to comment
Share on other sites

Do you really need to copy all word documents into a master document?
Or could you just add "links" to the master document. When opened the master document reads all linked documents and displays them as a single big document.

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

Hi Water,

That would work, providing that you could then save that one master document as a whole document in its own right.

Basically, I have ~ 120 different module documents.

and will have ~ 40 master documents, which are made up of different module documents.

The differences are captured in an excel matrix, utilising a standardised naming conventions for the module file names.

Each master document will need to be distributed (as a whole) for review and feedback, ideally in Doc or docx format to allow tracked changes etc.

Does that help?

Really appreciate your help on this one.

I read ages ago that there is an insert function somewhere, but that's all I can recall.

Link to comment
Share on other sites

Use _Word_DocRangeSet to set the insertion mark to the desired location.
The returned range object then is used to insert the file

$oRange.InsertFile("C:\temp\insert.docx")

 

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:

_Word_DocRangeSet($oDoc, -2)

 

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

:)

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'm not sure why, but the word documents aren't opening.

See the attached zip for all the dummy test files I'm using.

i wanted to create a fresh document, add the subdocuments and save it as a different file name at the end.

 

Ideally wanted to add each document by reading across the one row, then move to the next row.

Since that didn't seem to work, I transposed the data to then read down the column, then start on the next column.

#include <MsgBoxConstants.au3>
#include <Word.au3>
;~ #include <Array.au3>
#include <Excel.au3>
;~ #include <Timers.au3>
;~ #include <Date.au3>


;----------------------- CHANGE BELOW --------------------;
$nExcelBook = "matrix"
$cBookLocalName = @ScriptDir & "\Test\" & $nExcelBook & ".xlsx"
$version = "v1"
$vpn = False
$onerow = True
;----------------------- CHANGE ABOVE --------------------;
$columnstart = "A"
$rowstart = 4
$oWorkbook1 = 0

$eComment = "H"

OpenExcel()

;Local $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\Test\Test.docx", Default, Default, True)

;sleep(5000)
$run = 0
While 1
    $run = $run + 1
    $column = $columnstart
    $row = $rowstart
    if $run = 2 then $column = "B"
    Local $oRange, $oWord = _Word_Create()
    $eAreaName = _Excel_RangeRead($oWorkbook1, Default, $column & $row)
    If $eAreaName = "" Then
        _Excel_RangeWrite($oWorkbook1, $oWorkbook1.ActiveSheet, "Dataload completed", $eComment & $row)
        ExitLoop
    EndIf
    _Word_DocAdd($oWord)
    ; new word document
    ;insertdoc()
    _Word_DocRangeSet($oWord, -2)
    $oRange.InsertFile(@ScriptDir & "\Test\" & $eAreaName & ".docx")
    ;$column = $column + 1
    While 1
        $row = $row + 1
        $eAreaName = _Excel_RangeRead($oWorkbook1, Default, $column & $row)
        If $eAreaName = "" Then
            _Excel_RangeWrite($oWorkbook1, $oWorkbook1.ActiveSheet, "Row Completed", $eComment & $row)
            ExitLoop
        EndIf
        _Word_DocRangeSet($oWord, -2)
        $oRange.InsertFile(@ScriptDir & "\Test\" & $eAreaName & ".docx")
        ;insertdoc()
        ;$row = $row + 1
        ;$column = $column + 1
    WEnd
    $eAreaName = _Excel_RangeRead($oWorkbook1, Default, 1 & $row)
    _Word_DocSaveAs($oWord, @ScriptDir & "\Test\Final.doc");" & $eAreaName & ".doc")
    $rowcomplete = True
    If $onerow = True Then
        ExitLoop
    EndIf
    ;$row = $row + 1
WEnd

MsgBox(0, "Completed", "Document creator has completed.")

Func insertdoc()

EndFunc   ;==>insertdoc

Func OpenExcel()
    ; Create application object and open an example workbook
    _Excel_BookClose($oWorkbook1)
    Global $oExcel = _Excel_Open()
    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeCopy Example", "Error creating the Excel application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    ; Open Workbook 1
    Global $oWorkbook1 = _Excel_BookOpen($oExcel, $cBookLocalName)
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeCopy Example", "Error opening workbook '" & @ScriptDir & "Book1.xlsx'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
        _Excel_Close($oExcel)
        Exit
    EndIf
EndFunc   ;==>OpenExcel

 

Test.zip

Link to comment
Share on other sites

The following script works for me. Please copy all files into directory C:\temp\Merge_Word

Test2.zip

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

awesome.. so simple right?

However, can I make it read across columns in excel, and then say at the end (say column M), jump to the next row?

and how could i commence reading/inserting from column B?

and lastly, how do I insert a page break after each insertion?

 

not sure what : $wdPageBreak   would refer to?

$oRange.InsertBreak($wdPageBreak)

 

Link to comment
Share on other sites

Something like this?
$wdPageBreak is one of the Word enumerations you can find in the WordConstants.au3 (an include which gets called by Word.au3).

Test.zip

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

That's easy:

#include <Word.au3>
#include <Excel.au3>

Global $oExcel = _Excel_Open(False)
Global $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir & "\test.xlsx")
Global $aDocuments = _Excel_RangeRead($oWorkbook)
_Excel_Close($oExcel, False)
Global $oWord = _Word_Create()
Global $oDocMaster, $oDoc, $i, $j
For $i = 0 To UBound($aDocuments, 1) - 1
    $oDocMaster = _Word_DocAdd($oWord) ; Start a new document for every row
    Global $oRange = _Word_DocRangeSet($oDocMaster, -1)
    For $j = 0 To UBound($aDocuments, 2) - 2 ; last element of the row holds the path of $oDocMaster
        If $aDocuments[$i][$j] <> "" Then
            $oRange.InsertFile($aDocuments[$i][$j])
            $oRange = _Word_DocRangeSet($oDocMaster, -2)
            $oRange.InsertBreak($WdPageBreak)
        EndIf
    Next
    ConsoleWrite($aDocuments[$i][UBound($aDocuments, 2) - 1] & @CRLF)
    _Word_DocSaveAs($oDocMaster, $aDocuments[$i][UBound($aDocuments, 2) - 1], $WdFormatDocumentDefault) ; Save master document
    ConsoleWrite(@error & @CRLF)
    _Word_DocClose($oDocMaster) ; Close document
Next
_Word_Quit($oWord)

N.B. The Excel file needs to hold the full path to the inptu and output Word document e.g. C:\temp\Test1.docx

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

hi,

thanks for that :)

unfortunately i'm getting a error on the insert file function:

"C:\temp\wordmerge_v3.au3" (15) : ==> The requested action with this object has failed.:
$oRange.InsertFile($aDocuments[$i][$j])
$oRange^ ERROR

 

as you can tell in the data load file the names contain the file path of both the inserting and saving.

Any ideas?

Thanks again.

#include <Word.au3>
#include <Excel.au3>

Global $oExcel = _Excel_Open(False)
Global $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir & "\test.xlsx")
Global $aDocuments = _Excel_RangeRead($oWorkbook)
_Excel_Close($oExcel, False)
Global $oWord = _Word_Create()
Global $oDocMaster, $oDoc, $i, $j
For $i = 0 To UBound($aDocuments, 1) - 1
    $oDocMaster = _Word_DocAdd($oWord) ; Start a new document for every row
    Global $oRange = _Word_DocRangeSet($oDocMaster, -1)
    For $j = 0 To UBound($aDocuments, 2) - 2 ; last element of the row holds the path of $oDocMaster
        If $aDocuments[$i][$j] <> "" Then
            $oRange.InsertFile($aDocuments[$i][$j])
            $oRange = _Word_DocRangeSet($oDocMaster, -2)
            $oRange.InsertBreak($WdPageBreak)
        EndIf
    Next
    ConsoleWrite($aDocuments[$i][UBound($aDocuments, 2) - 1] & @CRLF)
    _Word_DocSaveAs($oDocMaster, $aDocuments[$i][UBound($aDocuments, 2) - 1], $WdFormatDocumentDefault) ; Save master document
    ConsoleWrite(@error & @CRLF)
    _Word_DocClose($oDocMaster) ; Close document
Next
_Word_Quit($oWord)

wordmerge_v3.au3

dataload2.xlsx

Edited by MrCheese
Link to comment
Share on other sites

Does this work for you? It ignores files which do not exist:

#include <Word.au3>
#include <Excel.au3>

Global $oExcel = _Excel_Open(False)
Global $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir & "\dataload2.xlsx")
Global $aDocuments = _Excel_RangeRead($oWorkbook)
_Excel_Close($oExcel, False)
Global $oWord = _Word_Create()
Global $oDocMaster, $oDoc, $i, $j
For $i = 0 To UBound($aDocuments, 1) - 1
    $oDocMaster = _Word_DocAdd($oWord) ; Start a new document for every row
    Global $oRange = _Word_DocRangeSet($oDocMaster, -1)
    For $j = 0 To UBound($aDocuments, 2) - 2 ; last element of the row holds the path of $oDocMaster
        If $aDocuments[$i][$j] <> "" And FileExists($aDocuments[$i][$j]) Then
            $oRange.InsertFile($aDocuments[$i][$j])
            $oRange = _Word_DocRangeSet($oDocMaster, -2)
            $oRange.InsertBreak($WdPageBreak)
        EndIf
    Next
    _Word_DocSaveAs($oDocMaster, $aDocuments[$i][UBound($aDocuments, 2) - 1], $WdFormatDocumentDefault) ; Save master document
    _Word_DocClose($oDocMaster) ; Close document
Next
_Word_Quit($oWord)

 

Edited by water

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

Another question:
We now have an empty page at the end of the master document. Should this empty page be removed?

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

This version deletes the last (empty) page:

#include <Word.au3>
#include <Excel.au3>

Global $oExcel = _Excel_Open(False)
Global $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir & "\dataload2.xlsx")
Global $aDocuments = _Excel_RangeRead($oWorkbook)
_Excel_Close($oExcel, False)
Global $oWord = _Word_Create()
Global $oDocMaster, $oDoc, $i, $j, $oRange
For $i = 0 To UBound($aDocuments, 1) - 1
    $oDocMaster = _Word_DocAdd($oWord) ; Start a new document for every row
    $oRange = _Word_DocRangeSet($oDocMaster, -1)
    For $j = 0 To UBound($aDocuments, 2) - 2 ; last element of the row holds the path of $oDocMaster
        If $aDocuments[$i][$j] <> "" And FileExists($aDocuments[$i][$j]) Then
            $oRange.InsertFile($aDocuments[$i][$j])
            $oRange = _Word_DocRangeSet($oDocMaster, -2)
            $oRange.InsertBreak($WdPageBreak)
        EndIf
    Next
    ; Go to the end of the document and delete the empty page
    $oRange = _Word_DocRangeSet($oDocMaster, -2, $wdCharacter, -3)
    $oRange.Delete
    ; Save and close master document
    _Word_DocSaveAs($oDocMaster, $aDocuments[$i][UBound($aDocuments, 2) - 1], $WdFormatDocumentDefault)
    _Word_DocClose($oDocMaster)
Next
_Word_Quit($oWord)

 

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

OK

so in the excel book, i added a column at the start which contained the names of some "base" document (title page, contents, revisions etc). (doc attached).

but now the cpu cycles up, the autoit takes much longer to complete and no output documents are found :(:( 

i did have blank cells, but I removed them as it wasn't working; but even after removal its not working.

also i noticed all my documents are .doc, and not docx. does this cause a problem?

dataload2.xlsx

NWoW-Deliverable-Template-Division-WS.doc

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

×
×
  • Create New...