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



< 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