• 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 35



In VBA, you can do this, as in the macro we're converting:

Set myRange = ActiveDocument.Paragraphs(1).Range

AmountMoved = myRange.MoveEnd(unit:=wdParagraph, Count:=1)

That sets myRange to the Range of the first paragraph, and then uses MoveEnd to move the end another paragraph on. Again, it does so "in place" – there's no need to redefine myRange, it's still there as a dynamic reference. So the MoveEnd Method has the "luxury" of being able to return the integer representing the number of characters this move has advanced.

In AppleScript, we do not have to create our own range this time. We can use move end of range on a text range set to the text object of the first paragraph, without crashing. However we still cannot modify the range "in place". Exactly as in the previous example, we have to set myRange, or another variable, to get hold of the new range returned by created by move end of range:

tell application "Microsoft Word"

     set myRange to text object of paragraph 1 of active document

     set myRange to (move end of range myRange ¬

          by a paragraph item count 1)

end tell

That's fine. But the necessity of the command to return the new range as the result means it cannot return the amount moved, as we have seen earlier. That is why so much of the script version is concerned with calculating this amount moved, which is not at all difficult (getting and redefining rangeEnd after every move) but a bit of a bother. As we shall see shortly, there is a simpler way to do it given the necessary difference in the AppleScript command from the VBA version..

Also, in the macro we're converting, at the end of the document you have to keep alert. In VBA when you try to do the final MoveEnd on the last paragraph mark, it doesn't error but simply returns 0. In AppleScript, move end of range also doesn't error but returns missing value (for a nonexistent new range that can't be made); that's a sort of null value. But the next line that attempts to get end of content of a non-existent range – that of course errors.

So we trap the error in a try/on error block and arbitrarily set the amountMoved variable to 0. (This is perhaps a somewhat silly way to do it, but I did so in order to keep the structure the same as for the VBA macro and to demonstrate that AppleScript has a repeat while loop too, although it's not used too often.)

You could re-do the script keeping all the move end and move start commands (and of course the delete) but simply omit all the get end of content and the amountMoved calculations: just leave all that out and depend on the trapped error at the end to close the script in a simple repeat loop with no while. New improved version, optimized for AppleScript:

tell application "Microsoft Word"

     --start with first paragraph and extend range down to second

     set myRange to text object of paragraph 1 of active document

     set myRange to (move end of range myRange by a paragraph item count 1)

    

     --loop until there are no more paragraphs to check

     repeat

          --if two paragraphs are identical, delete second one

          --and add the one after that to myRange so it can be checked

          if content of text object of paragraph 1 of myRange = ¬

              content of text object of paragraph 2 of myRange then

              delete text object of paragraph 2 of myRange



< 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