MS Word: Creating and selecting a range using bookmarks

Trying to figure out how to do this via Applescript.

Here’s the code I’ve got

tell application "Microsoft Word"
	
	set myRange to create range active document ¬
		start (start of content of text object of bookmark "begin_001") ¬
		end (start of content of text object of bookmark "end_001")
	
	select myRange
	
end tell

Which runs, but appears to do nothing (no selection).

I’ve double-checked that the bookmarks exist and enclose text between them.

What’s the correct syntax to accomplish this?

Well, found a permutation that works. Seems equivalent to what I had…go figure.

Here it is.

tell application "Microsoft Word"
	
	set bmBeginRange to (bookmark "begin_001" of active document)
	set bmEndRange to (bookmark "end_001" of active document)
	
	tell active document
		set my_range to create range start (start of content of text object of bmBeginRange) end (start of content of text object of bmEndRange)
	end tell
	
	select my_range
	
end tell

Just thought I’d mention: once you’ve got you’re selection, here’s how you grab it into a var:

set selectionAsText to content of text object of selection

I think you were over-complicating it. Try this:

tell application "Microsoft Word"
	tell active document
		set myr to ¬
			create range start (start of bookmark of bookmark "bm1") ¬
				end (end of bookmark of bookmark "bm2")
		
		select myr

	end tell
end tell

If you get the properties of a bookmark, you can see that it has it’s own start and end points which you can use to create the range. As an aside, a bookmark can encompass a range of text, so in this case, you could use a single bookmark that specified the entire range of text that you wish to select or otherwise work with.

tell application "Microsoft Word"
	properties of bookmark "bm2" of active document
	
	(* --> 
	{name:"bm2", text object:text object of bookmark "bm2" of active ¬
	document, empty:false, start of bookmark:894, end of bookmark:899, ¬
	column:false, story type:main text story}
	*)

end tell
1 Like

Yeah, that seems simpler and more to-the-point (though it sounds awkward if you speak it :slight_smile: )

Didn’t know about the ‘start of bookmark’ call.

Thanks

Yeah, there is a lot of redundancy in the verbiage but I guess they felt that without it, things would be even more confusing. It does give value to checking the dictionary/documentation out though.

1 Like