AppleScript MS Office – Set Text Style in MS Word via AppleScript

applescriptms office

I would like to find a way to change the style to heading1 of a particular text string that I'm adding in an MS Word document using Applescript, but after hours of searching, and looking at the dictionary, I can't find a way to to it. Something like this:

    tell application "Microsoft Word"
    activate
    tell active document
        set ContTemp to content of text object
        set StartRange to (count of ContTemp) - 1
        set endrange to StartRange
        set theRange to create range start StartRange end endrange
        set style of format of theRange to style heading1
        set content of theRange to StringVar
    end tell
end tell

But it generates an error at "set style of format of theRange. . . ". Removing that single lines works to add the text StringVar (a simple text string) into the Word document. But I can't style it, and I need to have it set to heading1.

Thanks for the help!!

Best Answer

If you open the Microsoft Word dictionary in Script Editor you will see that a range does not have an attribute format, only an attribute style so the correct line is set style of theRange to style heading1.

After that your code complains that StringVar is not defined so I assume you are setting it before the code you have given us.