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



4. Excel

Excel 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 Excel 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.

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. All the many examples have been written, and provided, by John McGimpsey, Macintosh MVP specialist in Excel. Some of his explanatory comments have been incorporated as well.

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 Excel" block, as its first line. That will bring Excel 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 Excel is in the front (and there may be other occasions when you are running a script from elsewhere and don't want Excel 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 Excel, it is a good idea to read the introductory section Using the Excel Dictionary and especially the section How to reference cells and ranges on p. 15, of the Excel 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 Excel's Object Model rather than vice versa, it is full of useful knowledge for all scripters of Excel.

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

Excel is one of the most complex and detailed applications there is, with a huge variety of capabilities. Rather than demonstrating a few sample scripts of a somewhat personal choice, this chapter will offer AppleScript translations of short commands of various types to illustrate a wide variety of techniques, such as you might find in various combinations in many macros. Credit for assembling and organizing these samples is fully John McGimpsey's. They should be of help in many different situations and lead you on to other areas of the AppleScript dictionary.

Most of the enormous repertoire of Excel features as programmed through VBA has a straightforward equivalent in AppleScript. But here and there are Methods or Properties that have not made it over, or not in the same format. When we come upon those, solutions and workarounds will be offered.

In particular, although the AppleScript implementation of Excel has been very thoroughly carried out, such that almost all of the VBA Model can be found mirrored in the AppleScript Model, there are some serious bugs connected with columns and rows. Make sure to read the relevant section of this chapter (Working with Columns and Rows) or you will run into trouble.

The one significant gap in the AppleScript model is that Worksheet Functions did not make it over from VBA. Apparently there are conflicts between how Excel performs these and how they would have to be done piped through AppleScript, which does not have the same precision with floating point numbers. But you can enter any Function (via AppleScript) into a cell on a worksheet, let Excel do its calculations, pass the result (cell value) back to the script, and clear the cell.

We will start, as with the other application chapters, with making and opening new documents (workbooks and worksheets), then pass on to working with existing worksheets, working with ranges, and then to a few of the many other features of Excel.

New Workbook, Open Workbook, Add a Worksheet

Make a new workbook

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

   Application.Workbooks.Add

is

make new workbook



< 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