Word + AppleScript - How to insert shape at location?

Hello -

TLDR; How do I insert a shape at a specific position within a block of text (a text range with the same start/end)?

Something like the Windows equivalent (https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.shapes.addtextbox.aspx?f=255&MSPPError=-2147217396)

I’ve been struggling with this one for several hours and I must be missing something.

OS: macOS 10.12.5
App: Word 2016 (15.33, 170409)

I’m trying to use AppleScript to insert a shape at a cursor position, but am unable to do so correctly. I’ve reviewed the 2004 AppleScript guide for Word, the Word scripting dictionary, and also Googled the heck out of samples.

As I gather, you should be able to “make new shape at [location]”, but I’m struggling with the [location] portion. I understand that there’s an anchor and you should be able to specify the anchor for the shape, then give the shape a position relative to the anchor.

Attempt 1: Specify “active document” (places box, but [as expected] not at selection position)

    
set shapeName to "my shape"
set theRange to text object of selection

set myShape to make new shape at active document with properties {shape type:shape type text box, anchor:theRange, top:0, left position:0, width:300, height:100, name:shapeName, relative horizontal position character, relative vertical position:relative vertical position paragraph}

This inserts the text box, but, it’s at the start of the document.

I thought/hoped that specifying relative position would let me say “basing the position on the anchor, make this box at (relative top) and (relative left)”. For the above code if I do specify a “top” (ex: 100), the box will be created 100 points below the start of the active page of the document. It doesn’t seem to be honoring the anchor as the reference starting point.

Attempt 2: Specify range (fails with error)

I thought that maybe by specifying “at active document” I was telling it to position the element relative to the page. I tried to tell it to position at the anchor (text range).

 
set shapeName to "my shape"
set theRange to text object of selection

set myShape to make new shape at end of theRange with properties {shape type:shape type text box, anchor:theRange, top:0, left position:0, width:300, height:100, name:shapeName, relative horizontal position:relative horizontal position page, relative vertical position:relative vertical position page}

This fails with the error: “Microsoft Word got an error: Can’t make shape type text box into type constant.”

I’ve tried a bunch of variations of positions of “theRange”, but I’ve not yet been successful at doing this. I’ve also tried specifying the selection instead of the range, but that also fails with the same error. I’m guessing you just can’t approach the shape this way.

Any insights very much appreciated.
Thank you!

I think that if you want it positioned at a certain point within text, rather than a certain position on the page, you want “inline shape” not “shape.”

Take a look at the dictionary for inline shape and see if that does what you’re looking for.

Also, page 365 of this:
https://applescriptlibrary.files.wordpress.com/2013/11/word-2004-applescript-reference.pdf

May be useful.