Pasting in word and switching the paragraph style

I would like to create a script that does this:

  1. pastes the text that is already in the clipboard in Microsoft Word.
  2. As the text is being pasted, remove all formatting.
  3. Select the text that was just pasted in word and just change the paragraph style to one called CODE I have in my Word.
  4. This CODE style has the option “Don’t add space between paragraphs of the same style” turned on, but when I paste unformatted text this option is always off. So, as a step 4, I need this option to be applied to the pasted text.

So far I have accomplished this:

tell application "Microsoft Word"
	paste special (text object of selection) data type paste text
end tell

This pastes text without formatting and covers steps 1 and 2.

What about steps 3 and 4?

Thanks

I think the simpest way to do it is to make the style change before you paste in the text. OMM, this also preserves the spacing.


tell application "Microsoft Word"
	activate
	set myWord to the clipboard
	tell text object of the selection
		set style to  style "CODE"
	end tell
	paste special (text object of selection) data type paste text
end tell


Thanks but this give me this error

AppleScript failed with script error text-script:34:35 expected “end” or “end tell” but found unknown token -2471 Paste no formatting.

You store the clipboard in myWord and never uses it and the problem is in the tell text line.

That’s odd. There seems to be a missing word: "Word style “CODE”

Try below.

Yes, I copied and pasted from another script and didn’t clean it all up.

tell application "Microsoft Word"
	activate
 	tell text object of the selection
		set style to Word style "CODE"
	end tell
	paste special (text object of selection) data type paste text
end tell

→ Mac OS Version 11.6.8 (20G730)
→ Script Debugger 8.0.5 (8A61)
→ Microsoft Word for Mac 16.67

1 Like

Thanks.

For some reason your script does not work for me.

This works for me

tell application "Microsoft Word"
	set style of (text object of the selection) to "CODE"
	paste special (text object of selection) data type paste text
end tell

Even simpler! Problem solved?