MacTech Network:   MacForge.net  |  Computer Memory  |  Register Domains  |  Printer Supplies  |  Cables  |  iPod Deals  |  Mac Deals  |  Mac Book Shelf


  MacTech Magazine

The journal of Macintosh technology

 
 
Create your own software

Magazine In Print
  About MacTech  
  Home Page  
  Subscribe  
  Archives DVD  
  Submit News  
  Submit a Tip!  
  Get a copy of MacTech RISK FREE  
Google
Entire Web
mactech.com
Mac Community
More...
MacTech Central
  by Category  
  by Company  
  by Product  
MacTech News
  MacTech News  
  Previous News  
  MacTech RSS  
Article Archives
  Show Indices  
  by Volume  
  by Author  
  Source Code FTP  
Inside MacTech
  Writer's Kit  
  Editorial Staff  
  Editorial Calendar  
  Back Issues  
  Advertising  
Contact Us
  Customer Service  
  MacTech Store  
  Legal/Disclaimers  
  Webmaster Feedback  

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.



 


Click here to find out more about our best subscription bundle deal ever!
2 years of the magazine, and the all new MacTech DVD ... at 70% off!



Click on the cover to
see this month's issue!

TRIAL SUBSCRIPTION
Get a RISK-FREE subscription to the only technical Mac magazine!
 
 


MacTech Magazine. www.mactech.com
Toll Free 877-MACTECH, Outside US/Canada: 805-494-9797

Register Low Cost (ok dirt cheap!) Domain Names in the MacTech Domain Store. As low as $1.99!
Save on brand compatible and name brank ink jet and laser supplies.
Save on long distance * Upgrade your Computer
Movies with No Late Fees!

See local info about Westlake Village
SJ * BRJ * BJ * OJ * NITS
Staff Site Links



All contents are Copyright 1984-2008 by Xplain Corporation. All rights reserved.

MacTech is a registered trademark of Xplain Corporation. Xplain, Video Depot, Movie Depot, Palm OS Depot, Explain It, MacDev, MacDev-1, THINK Reference, NetProfessional, NetProLive, JavaTech, WebTech, BeTech, LinuxTech, Apple Expo, MacTech Central 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.