Append styled clipboard content to end of document in Ms Word

I’m trying to write a script that copies the formatted content of cells in a web form and append it to a document that is open in Ms Word. (In a second script I want to return the adapted content of the Ms Word document to the cells in the web form.)

I have found (and adapted) this script to paste styled text to an Ms Word document:

--Paste styled clipboard content in Ms Word
tell application "Microsoft Word"
	paste special (text object of selection) data type paste rtf
end tell

Since there are a lot of cells to process, I’d like to avoid the activation of the Ms Word app (necessary for the pasting). I guess that a script that uses ranges will be faster. But that’s my untested assumption. (Besides the speed argument, the ‘screen flickering’ doesn’t look nice.)

My Question: How can I append the styled content of the clipboard to the end of a document that is open in Ms Word using ranges?

I’ve come up with this:

tell application "Microsoft Word"
	--tell active document
	--Paste at end of document
	collapse range text object of active document direction collapse end
	--paste object text object of selection
	--set clipboard to linefeed & clipboard & linefeed
	--set clipboard to clipboard & return
	--type linefeed
	paste special (text object of selection) data type paste rtf
	type return
	--type linefeed
	--end tell
end tell

My questions:

  • Why doesn’t this script work when ‘tell active document’ is present?
  • How can I make sure that every pasted clipboard context is in a separate paragraph? (Adding a linefeed to the clipboard obviously doesn’t work, nor does typing a return or linefeed.)

– Why doesn’t this script work when ‘tell active document’ is present?

See below. Your “collapse range” command had it’s own reference to tell active document, which is why that part worked, but the paste special is a command targeted at an app property, not a document property, which is why that didn’t work in a tell block.

–How can I make sure that every pasted clipboard context is in a separate paragraph? (Adding a linefeed to the clipboard obviously doesn’t work, nor does typing a return or linefeed.)

I don’t think “type” is the command you’re looking for. The dictionary has a “type text” command, and a “type paragraph” command, and one of those might work, but didn’t for me on first try.

But “make new paragraph at end” did work, just make sure it’s sent to the text object.

HTH

tell application "Microsoft Word"
	tell active document
		collapse range text object direction collapse end
	end tell
	paste special (text object of selection) data type paste rtf
	tell active document
		tell its text object
			make new paragraph at end
		end tell
	end tell
end tell

Thank you so much. Works like a charm. Very useful script that can be used for many purposes.