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


  MacTech Magazine

The journal of Macintosh technology

 
 

Magazine In Print
  About MacTech  
  Home Page  
  Subscribe  
  Archives DVD  
  Submit News  
  MacTech Forums  
  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 44



end tell

omitting the Dim statement, of course, because there is none in AppleScript.

The one optional argument for the Workbooks.Add method, the Template argument to open a new workbook based on a particular template, has no equivalent template parameter to the make new command. And there is no template property for workbooks to set at inception, either.

But there is a very simple solution that works perfectly. Unlike Mac Word 2004 and earlier, opening an Excel template – whether from Excel, or in the Finder, or via VBA, or via AppleScript – opens a new document (workbook) made from the template. So instead of make new document, just open the template. Thus

   Application.Workbooks.Add Template:="Mac HD:Folder:Template.xlt"

in VBA should be converted to:

tell application "Microsoft Excel"

     open "Mac HD:Folder:Template.xlt"

end tell

and that's all there is to it. If you need to set a variable (reference) to the new workbook, then instead of the basic open command from the Standard Suite – which does not return a result – use Excel's own open workbook command from the Excel Suite:

tell application "Microsoft Excel"

     set newWkbk to open workbook workbook file name ¬

          "Mac HD:Folder:Template.xlt"

end tell

This is the better solution.

Open an existing workbook

And that brings us around to how to open an existing document – which we've just done! As we can see, there are two ways to do it. All applications implement the Standard Suite's open command – it's one of the most basic commands there is, and is required of all Mac applications, even those otherwise unscriptable: open, run, print and quit are all required. Sometimes applications add their own specialized parameters to open, as Word does.

The problem is, that to keep it as flexible as possible, since it can also be used both by unscriptable applications and by the Finder to open any application's documents without having any conception how to represent them when opened, it does not return a result. That is not good for scriptable applications that open their own documents.

Excel does the smartest thing: it leaves the Standard Suite's open command in place – where it can be used to open entire lists of files at once – but with no special parameters. Instead, it also has a separate open workbook command in the Microsoft Excel Suite, equivalent to VBA's Open, with all of its parameters (arguments), and which does return a result. You can therefore set a variable (reference) to the open workbook command. That's the one you should use.

Workbooks.Open Filename:="Mac HD:Folder:File.xls"

translates to:

tell application "Microsoft Excel"

     open alias "Mac HD:Folder:File.xls"

     -- or:

     open "Mac HD:Folder:File.xls"

     -- or:

     open workbook workbook file name "Mac HD:Folder:File.xls"

end tell

while

   Set oWkbk = Workbooks.Open(Filename:="Mac HD:Folder:File.xls")

translates to:

tell application "Microsoft Excel"

     set theWkbk to open workbook workbook file name "Mac HD:Folder:File.xls"

end tell

The open workbook version offers many options. To open a text file you might find a macro with the Format argument, specifying the delimiter (1=Tab, 2=Comma, 3=Space, etc):

Workbooks.Open FileName:="Mac HD:Folder:File.txt", Format:=1

which translates to:

open workbook workbook file name ¬



 


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!
 
Nokia Qt Beta
 


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.