Moving from Microsoft Office VBA to AppleScript:
MacTech's Guide to Making the Transition
Introduction
|
Table of Contents
Page Prev and Page Next buttons at bottom of the page.
|
April, 2007
Page 34
End:=ActiveDocument.Paragraphs(3).Range.End
At the end, myRange is still defined, only now it will end at the end of the third paragraph. SetRange does not return a result, and does not need to: myRange has been modified "in place".
In AppleScript, you first need to understand that you can't set a variable myRange to the text object (range) of paragraph 1 and then try to extend that over more paragraphs, at least not with the set range command: Word will crash. (I believe that this may be a bug in set range, since it is not true for the move end of range command, as we have seen. So consider what follows a workaround for the bug, and one you need to know about.) myRange remains a reference to the first paragraph, and you cannot make it be something else simultaneously.
Instead create your own range set to the same start and end points, and modify that range instead:
tell application "Microsoft Word"
set par1Range to text object of paragraph 1 of active document
set myRange to create range active document start (start of content of par1Range) end (end of content of par1Range)
set myRange to set range myRange start (start of content of myRange) ¬
end (end of content of (get text object of paragraph 3 of active document))
content of myRange
end tell
That works fine – no crashing. myRange is set to the same dimensions as par1Range, but in terms of a start and an end point, not in terms of belonging to a particular paragraph. You can now set set range to redefine the end point, once again in terms of an integer, not paragraph 3 itself. set range returns a result that is a text range, but a new one. There is no modifying "in place". In order to continue referring to it as myRange, you need to redefine the variable myRange to that result. (Or you could set a different variable to the result. But if your VBA macro expects it still to be called myRange, your script should too, to minimize changes.) Now you can carry on.
An alternate workaround is just to avoid set range entirely:
tell application "Microsoft Word"
set par1Range to text object of paragraph 1 ¬
of active document
set par3Range to text object of paragraph 3 ¬
of active document
set myRange to create range active document start ¬
(start of content of par1Range) end ¬
(end of content of par3Range)
content of myRange
end tell
In the case of commands such as move end of range and move start of range, although they otherwise work just the same as MoveEnd and MoveStart in VBA, the fact that they have to return the modified range means that they cannot instead return the handy result of the number of characters moved as in VBA. That's not very hard to find – just keep getting the end of content of the text object of the range both before and after the move, and subtract one from the other to get the difference (amountMoved) as the same amount moved.
It also means you have to keep updating the variables for rangeEnd and newRangeEnd (set rangeEnd to newRangeEnd after performing the subtraction) or you'd "run out of variables". So it just takes a few more lines of code to do the same thing.
< Previous Page Next Page>
- SPREAD THE WORD:
- Slashdot
- Digg
- Del.icio.us
- Newsvine