How to get the selected text box in Word 16

Is there a reliable way to get the selected text box in Microsoft Word 16?
This snippet returns a missing value for the shape.
I have to manually set the shape as inline shape and then set it as some floating position (like ‘square’ or ‘in front of text’, etc.) to get it work.

-- this script is only for testing purpose and will not work with text boxes most of times
tell application id "MSWD"
	set shapeAnchor to (selection start of selection)
	set theShapes to (shapes of active document) whose (start of content of anchor = shapeAnchor)
	if theShapes = missing value then return "no shapes"
	set theShape to (item 1 of theShapes)
	set theContent to content of text range of text frame of theShape
	if theContent = missing value then return "no text"
	theContent
end tell

The problem is that the anchor is referenced inside the text box (selection start of selection = 0).

You have to adjust your parentheses a bit and move the container to the end of the statement.

	set theShapes to (shapes whose start of content of anchor = shapeAnchor) of active document

From Reference Forms > Filter in the Language Guide:

To specify a container after a filter, you must enclose the filter and the object specifier it applies to in parentheses, as in this example:

tell application "Finder"
    (files whose file type is not "APPL") in folder "HD:SomeFolder:"
end tell

On a separate note, the term selection needs to be clarified when it comes to text boxes. Tapping it so you can see its anchor or relocate its boundaries doesn’t appear to be the same thing as selecting it along with some text.

A shape differs from an inline shape — this is probably why your manual setting works.

Finally, the downside of this (beyond what you’re experiencing) is that it seems to be a chore to select but a single text box on a page which has more than one. Not saying it’s impossible… just that it isn’t superficially obvious.

This makes no difference.

How? If you mean selection of active window it does not help.

In this case, the text box can be retrieved… if there is only one shape in the selection.

I know that. Since a long time.

Anyway, thanks for trying to help…

I mean in the regular usage of the app. If you begin with nothing selected and single-click on a text box, a frame around the box will appear and you can do things like resize the box by clicking and dragging. As far as the script is concerned, this is not a selection.

You have to select some text on the page, seemingly around where the anchor is — it’s hard to know exactly what the requirements are. When I do this and run your script (after changing the syntax), I get a different outcome.


OK. Were are talking of the same thing: with the kind of selection you’re showing in your second screenshot, my script is returning a correct reference to the text box inside the selection.
But if there is more than one text box in the selection, there’s no way to determine which shape is selected.
That’s the crux of the matter.

It’s frustrating but you might be right. Most (all?) of the examples they provide are related to newly created text boxes and shapes which doesn’t really help when you’re attempting to work with existing elements. For all we know, there is a simple method to use but nobody knows what it is.

One potential indirect method might be to assign names to your text boxes. Then it might be possible to write code that would allow you to do something along the lines of:

if selected text box is x1 (i.e. first one on page with multiple) then set xy to text box whose name ends with '2' (i.e. second on page)