Copy/paste everything from 1 Word file to another

Short Version: I need to copy/paste the entire content of a Word file to a fresh, blank Word file. For reasons stated in the long version (below) anything like

insert file at text object of selection file name sourceFile

won’t do, it needs to be a real copy/paste.
I tried:

tell active document
	tell application "System Events" to keystroke "a" using {command down}
	tell application "System Events" to keystroke "c" using {command down}
end tell
close active document saving no
tell application "System Events" to keystroke "n" using {command down}
tell active document
     tell application "System Events" to keystroke "v" using {command down} 
end tell

but that doesn’t seem to work. The “new” (n) and the “paste” (v) command do work, but somewhere in the select all (a) or copy (c) sommthing is missing.

Any suggestions??

The long version: The reason for the need for copy/paste is in the conversion from footnotes to endnotes (or the other way around). We have a workflow that imports Word files in InDesign. In InDesign it’s not possible to do a simple conversion from foot- to endnotes. In Word you can, but after this conversion in Word Indesign will import the file as a blank, with nothing at all in the file. Except (you’ve guessed it) when (in Word) you do a select all/copy/paste of the text into a fresh new Word file. Save this fresh file, import it in InDesign, no problems…
For reasons we don’t understand ourselves only a real copy/paste will work.

Suggestions still welcome.

With kind regards,

Ton

Hi Ton,

certainly it’s possible to copy the contents of a word document to an empty new one
without UI scripting, but I don’t know word good enough :wink:

With UI scripting it needs to target an application process to perform the keystrokes.
This should work

tell application "Microsoft Word" to activate
tell application "System Events"
	tell application process "Microsoft Word"
		keystroke "a" using {command down}
		keystroke "c" using {command down}
		tell application "Microsoft Word" to close document 1 saving no
		keystroke "n" using {command down}
		repeat until (exists window 1)
			delay 0.5
		end repeat
		keystroke "v" using {command down}
	end tell
end tell

Ton:

I have done a few things related to what you need. This script will copy and paste the whole document into a new document:

tell application "Microsoft Word"
	set all_paras to count every paragraph of active document
	set myRange to create range active document start (start of content of ¬
		text object of paragraph 1 of active document) end (end of content ¬
		of text object of paragraph all_paras of active document) --Works OK, but does not like negative numbers
	select myRange
	set the_whole_thing to content of text object of selection
	set newDoc to make new document
	insert text the_whole_thing at end of text object of newDoc
end tell

Good luck,

Dear StefanK and Craig,

Thanks for you replies. Sorry for responding so late, but I’ve been ill for the last two weeks.

As for Craig’s method, this does’t work, because the end/footnotes are not selected, only the text that’s actually in paragraphs. This was the showstopper that led me to using the system events in the first place. The “select all paragraphs” in Word will do just that; select all text in all paragraphs. But foot and endnotes are by definition not in paragraphs…

Stefan’s method does work with a small adaptation in the delay part of the script.
Thanks for showing “the other way around” of doing system events. This “application process” method is totally new to me.

Both of you thanks for you input.

With kind regards,

Ton