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

 
 
Microsoft Office 2008

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 91



5. PowerPoint

PowerPoint 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 PowerPoint 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 subroutines (macros) in VBA, and their equivalent in AppleScript, with comments as necessary to explain differences in the AppleScript approach. All the examples are by Steve Rindsberg, PowerPoint MVP.

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 PowerPoint" block, as its first line. That will bring PowerPoint 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 PowerPoint is in the front (and there may be other occasions when you are running a script from elsewhere and don't want PowerPoint 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 PowerPoint, it is a good idea to read the introductory section Using the PowerPoint Dictionary of the PowerPoint 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 PowerPoint's Object Model rather than vice versa, and the introduction is quite short, it has some useful information for all scripters of PowerPoint. If you are going to be scripting text in text frames, you should turn now to Chapter 2: Word and read up on text range there, and also in the introductory section to the Word AppleScript Reference. But note that text frame (in the Drawing Suite of the dictionary), here in PowerPoint as well as in Word, is the one class that uses text range, rather than text object, as the name of its own range property as well as for the name of the text range class itself (in the Text Suite). (There are more details about this in the Find and Replace Text section of Chapter 2: Word.) Also check out the PowerPoint scripts on Apple's AppleScript website: <http://www.apple.com/applescript/powerpoint/>.

This is the point where it has to be acknowledged that, in comparison to Word and Excel, the PowerPoint 2004 AppleScript implementation leaves something to be desired. Not to put too fine a point on it, there really are quite a lot of bugs, and some gaps in the Object Model too. It may well be that PowerPoint "came in last" and didn't get a sufficiently exhaustive round of testing before Office 2004 was released on schedule. (There was one master developer who implemented the entire AppleScript implementation, from the ground up, for all of Office 2004, and he needed to finish before the specialist AppleScript testers got to it.) But these bugs are well known and hopefully most will be fixed for Office 2008, which is when you need these scripts after all.

In the meantime, if and when you hit any bugs that cannot be worked around while converting your scripts now, usually all you need do is substitute a line or more of do Visual Basic command, quoting your VBA lines as text in quotes. (You do not need any Dim declarations for these, even if you have your VB Editor set to Option Explicit, although you can include them if you wish.) do Visual Basic was the only way to script PowerPoint in Office v. X and still works fine in 2004, but will not work in Office 2008, at which point you will substitute the native AppleScript version.

If you already know what the proper AppleScript version should be (i.e., if the terms exist and compile, but don't function properly in 2004) you could include them commented out (i.e., preceded by -- dashes), and can try just uncommenting them and removing the do Visual Basic lines in PowerPoint 2008, and testing them out then. Or even put the do Visual Basic in a try block and the pure AppleScript in the on error block – but it's probably better not to second-guess a future implementation and see to it when the time comes. Remember that all quoting within the VBA string needs a backslash \ escape, like so: \". do Visual Basic can be a bit tricky with multiple-lines of VBA and with internal quoting; read my article on do Visual Basic, if you do need to use it, here: <http://word.mvps.org/MacWordNew/WordAppleScript.htm>. Although the article uses Word for its examples, it works exactly the same for PowerPoint. Hopefully, you will not need do Visual Basic at all, but it's there if you need it for the occasional line in PowerPoint 2004.

There's one particularly serious "gotcha" to beware: unaccountably, there is no selection object – either of the application or of the presentation – in PowerPoint 2004 AppleScript. Many VBA macros you have will be referencing the Selection Object, which leaves you stranded. Again, this will hopefully be rectified in PowerPoint 2008. In the meantime, although you can use do Visual Basic to get the Selection (and get particular slides and shapes via Selection.ShapeRange and Selection.SlideRange), do Visual Basic cannot return the result (the selected slide or shape) to AppleScript, so you would seem to be stuck. See if your macro can be rewritten to omit .Selection, which would be best.

Later in this article I provide temporary workarounds for 'selected slide' and 'selected shape' that will allow you to do your VBA macro conversions now in PowerPoint 2004 and make use of your scripts while waiting for Office 2008 to hopefully introduce the selection object to AppleScript. (If PowerPoint 2008 does not implement the selection object, or some alternate way to get selected shape(s) in AppleScript, then you will not be able to convert any macros involving selection or selected shapes in 2008, and would have to remain with PowerPoint 2004 (in Rosetta, if on an Intel computer), or use PowerPoint Windows in Parallels or VMWare, if these PowerPoint macros are necessary to you.)

New Presentation, Open Presentation, New Slide

Make a New Presentation

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

   Application.Presentations.Add

is

make new presentation

Just as in Word, this action – making the fundamental element of the PowerPoint application – is the one important make new command that does not let you set any properties at inception. 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 in Word. (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 presentations: properties cannot be set at inception of the document, which needs to be created first. This does correspond with how the Add Method works for Presentations, so it should not surprise people accustomed to VBA.



 


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