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.
|
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.
< Previous Page Next Page>
- SPREAD THE WORD:
- Slashdot
- Digg
- Del.icio.us
- Newsvine