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



   ActiveDocument.Bookmarks("myBookmark").Range.Text = "Inserted Text"

translates to:

tell application "Microsoft Word"

     set content of text object of bookmark ¬

          "myBookmark" of active document to "Inserted Text"

end tell

To insert text at an enclosing bookmark (one where you had selected some text to insert your bookmark), and then recreate the bookmark which would otherwise vanish:

Dim bmRange As Range

 

Set bmRange = ActiveDocument.Bookmarks("myBookmark").Range

bmRange.Text = "Inserted Text"

ActiveDocument.Bookmarks.Add _

Name:="myBookmark", _

Range:=bmRange

translates to:

tell application "Microsoft Word"

     set bmRange to text object of bookmark "myBookmark" of active document

     set content of bmRange to "Inserted Text"

     make new bookmark at active document with properties ¬

          {name:"myBookmark", text object:bmRange}

end tell

However, if you try that, you end up with a "placeholder" (insertion point) bookmark somewhere completely different in the document! This is once again because of AppleScript's (or, rather, the Word implementation's) inability to keep a variable to a dynamic range once it changes. The instant you change the text of the text range of the bookmark, your range bmRange goes up in a puff of smoke.

You can check that: insert

        properties of bmRange

as the second line inside the tell block, just after defining bmRange in the first line and end the script with an end tell right there. You will get an enormous record of dozens upon dozens of properties of this text range.

Then go back and insert the same line just after the original second line inside the block, i.e., just after setting the content of bmRange to "Inserted Text", and end the script there. You would expect to get the same enormous record of properties with the content property changed. Instead you get no result at all – bmRange does not exist any more. So you cannot use it to define the text object of your new bookmark.

Instead, get the start of content of bmRange before you alter it, (which is identical to the start of bookmark of the bookmark: you can verify that if you wish), measure the length (i.e., count) the string you are inserting, and add that number to the start of content result to get the new end of content, and end of bookmark, for the new bookmark. (The start of content is an insertion point whose number is the same as that of the character it comes after – recalling that the first insertion point and start of content in the document is 0, not 1 – so no subtraction of 1 is needed after adding the length of the new text to get the new end of content.)

Use these two numbers as start of bookmark and end of bookmark properties for making your new bookmark. (They also have the advantage that they are intended to be settable, whereas the text object property is marked as read-only.) Here's the script:

tell application "Microsoft Word"

     set bmRange to text object of bookmark "myBookmark" of active document

     set bmStart to bmRange's start of content

     set content of bmRange to "Inserted Text"

     set bmEnd to bmStart + (count "Inserted Text")

    

     make new bookmark at active document with properties ¬

          {name:"myBookmark", start of bookmark:bmStart, end of bookmark:bmEnd}

end tell

This works perfectly. It has had to be adapted from the straightforward translation from the VBA that we first tried, after checking it and thinking about the cause of the problem (the problem with ranges being non-dynamic in AppleScript). That led to an alternative approach, using some of the many, many properties, classes and commands available. There's almost always a solution.

It should be quite evident by now that although most of the Word Object Model transfers over quite nicely from VBA to AppleScript, there are quite a number of differences in how VBA and AppleScript work – both in trying to shape the syntax into effective statements, but also sometimes in how (near-)equivalences had to be implemented by the Word developers too, so that a certain degree of working things out, and working around unexpected snags, is part of the process of converting your macros.

That is why I have tried to explain in some detail the why as well as the how, since you will undoubtedly hit other issues in other parts of Word's vocabulary and will need to understand the problems. If you have a sense for why things work as they do, you will undoubtedly also find the solutions.



< 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