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



     end tell

  

     insert file at text object of newDoc file name templatePath

end tell

There are a few interesting lines in this longer version. The open as document command opens a template as a document, so now all of document's properties, including page setup, are available. The next line gets a very special property of page setup (and of almost all classes in all the Office applications) – the properties property. That returns a record of all its properties. It's not only very efficient, sending just one AppleEvent instead of 30 separate ones, but it saves having to specify each one in the script and set a variable to each.

One of page setup's properties is a reference – its line numbering property – so we get all its properties too. Otherwise when we try to set another document's page setup properties to this properties record, it would balk at being set to another document's item. Then we close the template, having garnered from it all the information we need.

Then comes the rather amazing bit – being able to set newDoc's page setup's properties to the variable containing the whole properties record, and the same for the line numbering properties. The reason why it's amazing – aside from saving us the trouble and effort of specifying 30 properties by 30 more variables – is that the 30 properties contain one – the class – that's read-only. Fortunately that doesn't error but is just ignored. (The class is already correct in any case.) It's just the same for the line numbering properties. Then we insert the template file, and everything is as it should be.

Still, it may be a nuisance to have to turn off the macro alert warning, or to have to put up with it, while still using Word 2004, and you may not like the template opening up either. So for now, while still in 2004, you could use a completely different workaround, which will not work in Word 2008 but should not have to.

You can substitute:

do Visual Basic "Documents.Add Template:=\"Mac HD:" & ¬

"Applications:Microsoft Office 2004:My Templates:Test Template.dot\""

for the make new document with properties statement, and your scripts converting macros that use Documents.Add Template will work in Word 2004. Of course, the do Visual Basic command will stop working in Word 2008, along with VBA itself (it too needs the VBA compiler), and you'll need to switch over to native AppleScript at that time.

If all goes according to plan something like this should even work both now and then (if compiled and saved now, in 2004):

tell application "Microsoft Word"

     try

          do Visual Basic "Documents.Add Template:=\"Mac HD:" & ¬

              "Applications:Microsoft Office 2004:My Templates:Test Template.dot\""

     on error

          make new document with properties {attached template:¬

              "Mac HD:Applications:" & ¬

              "Microsoft Office 2004:My Templates:Test Template.dot"}

     end try

end tell

But it's better to wait until the new version comes out and then test it. After all, you will probably have a new location for your template then – it's not likely to still be in the Microsoft Office 2004 folder. The do Visual Basic command, in a script saved from 2004, might not error, it might just not do anything. If you try to compile or edit it in 2008, it probably will not even compile. The syntax for setting the template of the document may well turn out to be different than anticipated here. So just consider this a workaround for 2004. Check it out properly when the time comes, in Word 2008.

Open an Existing Document

It's a relief to say that opening an existing document is dead simple in AppleScript. The equivalent of the Open Method in VBA:

   Documents.Open FileName:="Mac HD:Folder:Filename.doc"

is the open command (in the Standard Suite) in AppleScript:

tell application "Microsoft Word"

     open alias "Mac HD:Folder:Filename.doc"

end tell

Note that you should properly use the alias reference form, but the Word developers have enabled a coercion allowing you to use just the path text as an equivalent, just as in VBA. Also, in AppleScript, you can open a list of documents all at once if you wish.

Sometimes we are interested not just in opening a document, but also in modifying it, or getting some information from it, of course. In VBA, that is very straightforward: you just set a variable (reference) to the result of the Open statement, which returns a Document object, and then set some Properties of it or apply some Methods to it:



< 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