Selecting a sub-range from a range based on bookmarks in MS Word

I have a range defined by bookmarks which properly returns a passage of text:

set myRange to ¬
   (create range active document start (start of bookmark of bookmark beginRng of active document) ¬
	   end (start of bookmark of bookmark endRng of active document))

And I’d like to create a sub-range from that range limited to a certain amount of paragraphs/words/characters.

To grab just the first 5 paragraphs, I’ve tried these 2 statements–both return nothing (‘missing value’ in content field):

set mySubRange to ¬
	(create range active document start (paragraph 1 of start of bookmark of bookmark beginRng of active document) ¬
		end (paragraph 5 of start of bookmark of bookmark beginRng of active document))

set mySubRange to ¬
	(create range active document start (paragraph 1 of myRange) ¬
		end (paragraph 5 of myRange))

Anyone know the secret combo here?

Like so:

tell application "Microsoft Word"
	tell active document
		set myRange to get story range story type main text story 
		-- or however you want to get it
		set subRange to ¬
			create range start (start of content of text object of paragraph 1 of myRange) ¬
				end (end of content of text object of paragraph 5 of myRange)
	end tell
end tell
2 Likes

Hi applesource.

It would help if you say what application you’re trying to script, especially in the subject line of an enquiry.

Thanks. Thought I had tried that.

It works…with one modification:

set subRange to ¬
	create range active document start (start of content of text object of paragraph 1 of myRange) ¬
		end (end of content of text object of paragraph 5 of myRange)

Without ‘active document’ it fails with “Error: Can’t continue create range.”

You’re right. My bad for not including

tell application "Microsoft Word"

Subject Line fixed.

If it’s wrapped in a tell active document block, as I did above, that’s not necessary because the direct object is already set.

Confirmed :flushed:

Missed the ‘tell’.

FOLLOW-UP question

Though getting a sub-range using paragraphs works fine, substituting ‘words’ or ‘characters’ for ‘paragraphs’ does not.

These both return ‘missing value’ in the content field:

set subRange to ¬
	create range active document start (start of content of text object of word 1 of myRange) ¬
		end (end of content of text object of word 500 of myRange)

set subRange to ¬
	create range active document start (start of content of text object of character 1 of myRange) ¬
		end (end of content of text object of character 500 of myRange)

Is there a way to set a sub-range with words or characters?

Use start of content of word 1 of myRange and end of content of word 500 of myRange etc instead. word and character don’t have a text object property like paragraph does.

set subRange to ¬
	create range active document start (start of content of word 1 of myRange) ¬
		end (end of content of word 500 of myRange)

set subRange to ¬
	create range active document start (start of content of character 1 of myRange) ¬
		end (end of content of character 500 of myRange)
1 Like

Remove the text object of from each start and end.

If you check the properties of any of the related items you’re working with here (text ranges, contents of…, text objects, etc…) and see whether they include a start of contents property. Actually, you can just start of content of myrange or start of content of word 5 of myrange. If it returns the appropriate integer, then it’s valid. If it returns missing value, then it’s not.

Finally, to follow up on that, all that the start of content and end of content parameters require is an integer indicating how many characters into the page you want the range to extend to.

So if you know that you want character 500, then you could just use 500, and any method that gets you that integer is fine.

One more finally… there is a set of commands that allow you to manipulate the beginning and end of a text range. A relevant example is move start of range.

An example of use would be something like this:

set bigRge to (create range active document start (start of bookmark of begRge) end (end of bookmark of endRge))
set littleRge to bigRge -- duplicate the reference to the text range, this will be the sub-range

-- move the beginning of the sub-range to 3 paragraphs later	
set littleRge to move start of range littleRge by a paragraph item count 3
-- move the end of the sub-range to the second last paragraph
set littleRge to move end of range littleRge by a paragraph item count -2

If you look this up in the dictionary, you will see that you can also use characters or words to relocate the range bounds.

1 Like

:bulb:

That clarifies things.

This works, thanks.