MS Word: import styles using 'copy styles from template'

Sharing this snippet.

I found the ‘copy styles from template’ command in MS Word AS Reference. Couldn’t find any examples online, so figured it out, and am posting one here.

.

-- define template
property documentTemplate : "~/Documents/TemplateExample.dotx"

tell application "Microsoft Word"
	activate
	tell active document
		
		-- import styles from template
		copy styles from template template documentTemplate
		
		-- select range of text
		set titleRng to ¬
			create range start (start of content of text object of paragraph 1) ¬
			end (end of content of text object of paragraph 1)

		-- apply imported style to text range
		set (style of titleRng) to "Title"

	end tell
end tell

Since the paragraph class has a style property, you can also just do this without needing to construct a text range:

-- define template
property documentTemplate : "~/Downloads/2024-11-03/Industry manager resume.docx"

tell application "Microsoft Word"
	activate
	tell active document
		
		-- import styles from template
		copy styles from template template documentTemplate
		
		-- apply imported style to paragraph
		set style of paragraph 1 to "Title"
		
	end tell
end tell

And if you already have styles applied to your document and you just want to update them to match the corresponding style from the new template, you don’t even need to do anything except copy them in; Word will automatically update the existing styles.

1 Like