Applying word styles (with AppleScript?)

Word 'styles' utlise paragraph formatting in addition to text formatting, so I am trying to create a macro where I copy four different strings to the clipboard, and it opens Word and pastes them with the correct style applied. I imagine this can only be done with applescript, if Word supports that. Can anybody give me an idea if it can be done before I start down another automation rabbit hole?

I could do the text formatting with KM, but say adding extra space after the paragraph is only in word. I'm using 'Heading 1' 'Heading 2' etc.

I'm not having a problem with the copying and pasting, just the word styles element (so far!) :wink:

Thanks in advance.

Microsoft Word for Mac V. 15.27
KM 8.2.4.

If you want to use actual Word Styles (as opposed to text formatting by KM), then you will need to paste each string into Word, and then apply the Word style using either VBA or AppleScript. Since this is totally within Word, I would first try Word VBA, since you can easily run the Word Macro recorder and manually enter the steps to create the VBA Macro.

As you noted, Word Styles are applied to paragraphs, so I assume each of your strings will be a paragraph. So, after you paste each string (which does NOT end in a new line character), just run a Word VBA Macro that applies the style. You may want to use AppleScript to either run the macro, or just execute the same command as the VBA shown below.

The VBA is very simple. I just recorded this in Word 2011:

Sub Apply_Style()
'
' Apply_Style Macro
'
'
    Selection.Style = ActiveDocument.Styles("Intense Quote")
End Sub

Thanks so much @JMichaelTX

It took some work but I got it to work. Turns out the best way seems to be to set the style first, and then paste the text, rather than pasting and then trying to style it, due to carriage returns and paragraphs. Which you predicted of course with your post.

1 Like

It would be great if you could post/upload your script/macro to do this. I'm sure others would find it useful.

Here you go:
To use - copy name, then affiliation, then title, then abstract.
Then run macro. My word document title contained text 'all abstracts' - you can change as appropriate

Keyboard Maestro 8.2.4 “Copy from document to Word with style” Macro

Copy from document to Word with style.kmmacros (11 KB)

1 Like