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

 
 
The Cross Platform Security Leader

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 19



3. Word

Word VBA Macros to AppleScript

Make sure you have read Chapter 2: AppleScript in Microsoft Office (General) first. It deals with many important features of VBA, including Word VBA, that need to be treated very differently in AppleScript, as well as some equivalent control and operator statements in AppleScript you need to know. All examples in that chapter, as it happens, are for Word.

This chapter consists of typical, and popular, subroutines (macros) in VBA, and their equivalent in AppleScript, with comments as necessary to explain differences in the AppleScript approach. Almost all the examples come from

<http://www.word.mvps.org/FAQs/MacrosVBA/>

(Note: If you are using Safari as your browser, the page may come up blank initially. Refresh the page a few times to see it, and you may need to do the same for other www.word.mvps.org links.) Jonathan West, Word MVP, who also wrote some of them, suggested all the examples.

Note: when testing out scripts in Script Editor, you may wish to begin every one with an activate command on its own line just inside the tell application "Microsoft Word" block, as its first line. That will bring Word to the front so you can see what is happening. It is not necessary to include activate when running saved scripts from the script menu while Word is in the front (and there may be other occasions when you are running a script from elsewhere and don't want Word to come to the front). I have not included activate in the sample scripts here, but you may wish to do so.

Before launching into scripting Word, it is a good idea to read the introductory section Using the Word Dictionary and especially the section Working with text range objects on p.18, of the Word AppleScript Reference available in PDF format free from Microsoft at <http://www.microsoft.com/mac/resources/resources.aspx?pid=asforoffice>. Although it is largely aimed at AppleScripters unfamiliar with Word's Object Model rather than vice versa, it is full of useful knowledge for all scripters of Word.

Remember that this article was written while Office 2004 is current: some problems with Word 2004's AppleScript discussed here may be fixed in Word 2008 and workarounds provided here may no longer be needed there.

Note that although the class name in AppleScript for VBA's Range Object is text range (in the Text Suite), the name of the property corresponding to the Range Property in countless classes is almost always called text object (returning an object of text range class). Similarly the font class is the "type" of the font object property in other classes. There is a very good reason why application developers avoid using the same name for a class as for a property, which is explained below in the Find and Replace Text section, and in more detail in Chapter 6: Entourage.

Also very important to understand, since you may have to work around it in your scripts, is that text ranges you define are not "dynamic": you can manipulate them at will, but if you change their start or end positions or their content, the variable you may have set to the range will now not return the altered range, and you need re-set the variable to the result returned, if it is the altered range, or get hold of it some other way.

This is an unfortunate by-product of how Range had to be implemented in AppleScript, and may be a thorn in your side when you come to convert your macros, but there is virtually always some way to work around the changes. Two sections below – Delete duplicate paragraphs using a Text Range object and Working with Bookmarks – both go into this issue in some detail, with examples.

New Document , Open Document, and Templates

Make a New Document

Just a reminder, already mentioned in Chapter 2, that the way to make a new blank document equivalent to

      Application.Documents.Add

is

make new document

Normally in AppleScript, you would expect to be able to specify many, or most, of the properties of document in a with properties parameter, right at creation time, as in Chapter 2's example of making a new table at a document. (A scripter would expect that only elements, if any are needed, would have to be added afterwards.) But that is not the case with making new documents in Word: properties cannot be set at inception of the document, which needs to be created first before they can be altered.

This seems to be an "Office special" and corresponds with how the Add Method works, so should not surprise people accustomed to VBA, although it would surprise a regular AppleScripter accustomed to other applications. In the same way that in VBA the only arguments that can be specified with Documents.Add are Template, NewTemplate, DocumentType, Visible (more on templates in a moment), but not any of the 50 or so Properties of Document itself, which are specified later, so it is with AppleScript.

For example, in VBA, to set the font and a bit of content, you would do this:

Set NewDoc = Documents.Add

With NewDoc.Content

      .Font.Name = "Arial"

      .Text = "Here is some Text."

End With

An AppleScripter would expect to be able to do this:

tell application "Microsoft Word"

     set newDoc to make new document with properties {text object



 


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.