'move range end until' in Microsoft Word

The script below works just fine IF the myDoc file is the active document in Word. But if it is not, the rangeObj1 ends up refering to whatever document happens to be the active document, despite the explicit creation of the rangeObj1 using myDoc in the first place. I found through the commented out testing lines that the offending line is the one involving ‘move range end until’. (That’s where there is a change in the content sent back.) I also discovered that if you move this same code up to the main code, it again works just fine. Of course I want to use the subroutine to reduce writing. I even tried sending myDoc down into the subroutine and creating a new rangeObject before the offending line, but the offender still refered back to Word’s active document again. And I know I can just make myDoc the active document and avoid the problem altogether. But that means anything that calls moveCursorAfterThisNextChar will NEED to always do this. I was hoping to make the subroutine a little more bullet-proof than that.

Any one know why this is happening and if there a fix or better way?

set myDoc to "9990 - Script Listings"
tell application "Microsoft Word"
	set rangeObj1 to create range document myDoc start 0 end 1
end tell -- application "Microsoft Word"
set rangeObj1 to moveCursorAfterThisNextChar(tab, rangeObj1)
set x to properties of rangeObj1

to moveCursorAfterThisNextChar(inChar, rangeObject)
	-- v1.00
	-- Independent
	
	tell application "Microsoft Word"
		set m to start of content of rangeObject
		--set x to content of rangeObject -----------> Just for testing
		set rangeObject to move range end until rangeObject characters inChar count go forward
		--set x to content of rangeObject -----------> Just for testing
		set n to end of content of rangeObject
		set rangeObject to set range rangeObject start m end n
		set output to rangeObject
	end tell -- application "Microsoft Word"
	--set output to x -----------> Just for testing
end moveCursorAfterThisNextChar

Thanks Jacques! Right on the money! :slight_smile: It took me a little bit to figure out what dictionary you meant and where in it to look, but I think I see what you’re saying. Let me see if I got this right. The Word2004AppleScriptRef.pdf told me that in this code,

set rangeObject to move range end until rangeObject characters inChar count go forward

the ‘move range end until’ is actually doing what it is supposed to be doing which is finding the next tab in the document that I want. The problem is that in setting rangeObject to the result, it’s creating a new text range object that apparently does not transfer where the original text range came from. And this makes sense since ‘document’ is not a property of the text range object. And since no document is being specified, it is defaulting to the active document. In fact, in looking back at the ‘end of content’ in the results where I was getting my error, I now noticed that is was getting the position of the tab correct as if it was looking at the document I wanted. And the active document didn’t have a tab at that ‘end of content’ location showing that the ‘move range end until’ wasn’t really looking at it. Your code uses the ‘move range end until’ to find the correct start and end of contents, and then uses those to readjust the original text range object. (I expanded your beautifully succinct line in the code below to verify what it was doing for myself - but yours is better. :))

And you’ve actually answered another nagging question too. I was wondering what the “my” was about before the handler calls. I see now that it’s telling Microsoft Word to ignore my handler as one of its own and allowing the execution flow to continue into the handler despite being in Word’s tell block. But I did try taking it back outside the tell block and what a difference. For whatever reason, the call inside Word’s tell block takes significantly more time to execute than outside (see code below). No idea why.

But thanks for the answers Jacques. :slight_smile:

set myDoc to "9990 - Script Listings"
tell application "Microsoft Word"
	set rangeObj1 to (create range document myDoc start 0 end 1)
end tell
set rangeObj1 to moveCursorAfterThisNextChar(tab, rangeObj1)
set x to properties of rangeObj1

to moveCursorAfterThisNextChar(inChar, rangeObject)
	tell application "Microsoft Word"
		set range2 to move range end until rangeObject characters inChar count go forward
		set m to start of content of range2
		set n to end of content of range2
		set range rangeObject start m end n
	end tell -- application "Microsoft Word"
end moveCursorAfterThisNextChar