Applescript write on MS Word

Browsing for something similar I found this question, which ended with a different solution (Not using MS Word). My goal is to develop a script that writes some text in MS Word which, after copying the text they wanted to copy, is the same question as that post. Turns out it is fairly simple to do it. I post here (as answer) the script to write (or paste text) in a MS Word in case anyone needs it.

Accepted Reply

Adapting and simplifying this guide I obtained the following code which does the work:


set textVar to "Example first line" & return & "Example second line" --create text variable

tell application "Microsoft Word" --telling MS Word to create a new document, as opposed to "active document"
     create new document

     set myRange to create range active document start 0 end 0 --now we are setting what the contents of text, for the whole document (myRange), should be, by calling our variable (fiHR) which we have previously defined

     set content of myRange to textVar --this line sets our cursor/insertion point to the end of our text. it could be set to a character, word, line paragraph etc. Story is everything we have typed into the new doc. We do this, because we want the formatting to apply to all of the text typed in from our variable.

endtell

Replies

Adapting and simplifying this guide I obtained the following code which does the work:


set textVar to "Example first line" & return & "Example second line" --create text variable

tell application "Microsoft Word" --telling MS Word to create a new document, as opposed to "active document"
     create new document

     set myRange to create range active document start 0 end 0 --now we are setting what the contents of text, for the whole document (myRange), should be, by calling our variable (fiHR) which we have previously defined

     set content of myRange to textVar --this line sets our cursor/insertion point to the end of our text. it could be set to a character, word, line paragraph etc. Story is everything we have typed into the new doc. We do this, because we want the formatting to apply to all of the text typed in from our variable.

endtell