• MacTech Network:
  • Tech Support
  • |
  • MacForge.net
  • |
  • Apple News
  • |
  • Register Domains
  • |
  • SSL Certificates
  • |
  • iPod Deals
  • |
  • Mac Deals
  • |
  • Mac Book Shelf

MAC TECH

  • Home
  • Magazine
    • About MacTech in Print
    • Issue Table of Contents
    • Subscribe
    • Risk Free Sample
    • Back Issues
    • MacTech DVD
  • Archives
    • MacTech Print Archives
    • MacMod
    • MacTutor
    • FrameWorks
    • develop
  • Forums
  • News
    • MacTech News
    • MacTech Blog
    • MacTech Reviews and KoolTools
    • Whitepapers, Screencasts, Videos and Books
    • News Scanner
    • Rumors Scanner
    • Documentation Scanner
    • Submit News or PR
    • MacTech News List
  • Store
  • Apple Expo
    • by Category
    • by Company
    • by Product
  • Job Board
  • Editorial
    • Submit News or PR
    • Writer's Kit
    • Editorial Staff
    • Editorial Calendar
  • Advertising
    • Benefits of MacTech
    • Mechanicals and Submission
    • Dates and Deadlines
    • Submit Apple Expo Entry
  • User
    • Register for Ongoing Raffles
    • Register new user
    • Edit User Settings
    • Logout
  • Contact
    • Customer Service
    • Webmaster Feedback
    • Submit News or PR
    • Suggest an article
  • Connect Tools
    • MacTech Live Podcast
    • RSS Feeds
    • Twitter

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.

Would you like a hard copy
or PDF of this Guide?

You can get a hard copy sent to you
AND download a PDF now ($19.95)
, or

... just download a PDF ($9.95).

Either way, you get a complimentary
MacTech Magazine Subscription

courtesy of the
Microsoft Mac Business Unit


 

  Magazine Reg. Price:  $50.00 value  
  Guide Reg. Price:  $40.00 value  
  You Save:  over $80.00!  

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>
 
MacTech Only Search:
Community Search:

 
 
 

 
 
 
 
 
  • SPREAD THE WORD:
  • Slashdot
  • Digg
  • Del.icio.us
  • Reddit
  • Newsvine
  • Generate a short URL for this page:



MacTech Magazine. www.mactech.com
Toll Free 877-MACTECH, Outside US/Canada: 805-494-9797
MacTech is a registered trademark of Xplain Corporation. Xplain, "The journal of Apple technology", Apple Expo, Explain It, MacDev, MacDev-1, THINK Reference, NetProfessional, Apple Expo, MacTech Central, MacTech Domains, MacNews, MacForge, and the MacTutorMan are trademarks or service marks of Xplain Corporation. Sprocket is a registered trademark of eSprocket Corporation. Other trademarks and copyrights appearing in this printing or software remain the property of their respective holders.
All contents are Copyright 1984-2010 by Xplain Corporation. All rights reserved. Theme designed by Icreon.
 
Nov. 20: Take Control of Syncing Data in Sow Leopard' released
Nov. 19: Cocktail 4.5 (Leopard Edition) released
Nov. 19: macProVideo offers new Cubase tutorials
Nov. 18: S Stardom anounces Safe Capsule, a companion piece for Apple's
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live