Microsoft Word - Appending Files

This forum has been a tremendous help to me and I really appreciate the input from many of you.

To my question, I have a script that appends doc files vertically. I have implemented a table view whereby the doc files can be reordered and set as aliases. So, for example, these files are ordered from top–> bottom : 1.doc, 2.doc, 3.doc. A page break is inserted between each file.

However, using the code below, the files are appended in reverse (3.doc is first, not last).


on Merge()
	
	set theItems to aliasesOfMainDataSource()
	
	set x to 0
	set theOutputPath to (path to desktop folder as string) & "MasterDoc.doc"
	tell application "Microsoft Word"
		activate
		make new document
		repeat with aFile in theItems
			tell application "Finder"
				set fileRef to ((a reference to file aFile) as string)
			end tell
			insert file at text object of selection file name fileRef
			if x < ((count of theItems) - 1) then
				insert break at text object of selection break type page break
			end if
			set x to x + 1
		end repeat
		save as active document file name theOutputPath
	end tell
end Merge


This script was extracted from: http://www.macosxhints.ch/files/wordkombinieren.txt

I’d appreciate some guidance and thanks once again to all on this forum.

I’m not sure why it’s going backwards, but:

reverse of theItems

Place this before your loop, and it should fix the problem.

-SuperScripter

Hi,

I tried placing the reverse statement before the repeat line and getting the same result…

Hi,

the easiest way is to process the list backwards, particularly you need an index variable anyway


on Merge()
	set theItems to aliasesOfMainDataSource()
	
	set theOutputPath to (path to desktop folder as text) & "MasterDoc.doc"
	tell application "Microsoft Word"
		activate
		make new document
		repeat with i from (count theItems) to 1 by -1
			insert file at text object of selection file name (item i of theItems as text)
			if i > 1 then insert break at text object of selection break type page break
		end repeat
		save as active document file name theOutputPath
	end tell
end Merge

note: the Finder is not needed to coerce an alias to a string path

Stefan,

You did it again - this worked perfectly. You’re truly an asset to this forum. Thank you again. Now, I have to go figure out what your modified code is doing.

Best regards,
Marlon