MS Word: select range within a range, and apply formatting

I’m attempting to create a range of words within an existing range, and then apply formatting to this sub-range.

This code looks right and compiles, but does nothing.

What’s the right syntax?

set summaryRng to ¬
	(create range start (start of bookmark of bookmark "summary_begin") ¬
		end (end of bookmark of bookmark "summary_end"))

set wordSubRng to ¬
	(create range start (first word of text object of paragraph 1 of summaryRng) ¬
		end (second word of text object of paragraph 1 of summaryRng))

set underline of font object of wordSubRng to true

You aren’t telling Word where to start and end wordSubRng correctly. You need to use the start of content and end of content properties of the words you wish to underline.

set summaryRng to ¬
	(create range start (start of bookmark of bookmark "summary_begin") ¬
		end (end of bookmark of bookmark "summary_end"))

-- use start/end of content properties
-- and you can simplify it too
set wordSubRng to ¬
	(create range start (start of content of word 1 of summaryRng) ¬
		end (end of content of word 2 of summaryRng))

set underline of font object of wordSubRng to true
1 Like

At first, I couldn’t get this code to work. Then, I made a discovery about setting the contents of a bookmark text range.

Setting contents of bookmark range like this:

set content of summaryRng to (summary)

does NOT place text between the _begin and _end bookmarks. All text shows up after the _end bookmark. I think this is why applying formatting to the sub-range didn’t work.

But, if I place text in the range like this:

insert text summary at summaryRng

the bookmarks expand to enclose the text, and applying formatting to the sub-range then works.