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



          {font object:{name:"Arial"}, content:"Here is some text."}}

end tell

(Even though text object is listed in the dictionary as read-only, such properties can usually be set at inception.) But all you get is a blank new document if you try it that way. Instead, you need to do it "VBA-style", setting properties of the document's text object after the fact:

tell application "Microsoft Word"

     set newDoc to make new document

     tell newDoc's text object

          set name of font object to "Arial"

          set content to "Here is some text."

     end tell

end tell

What about those four arguments that can be set with VBA's Documents.Add? I doubt you will ever run into .Visible = False in a macro, but if you should, you won't be able to make an invisible document in AppleScript. You can set the collapsed property of the document's window 1 to true if you wish, which minimizes it, once you have created the document.

You cannot preset a new document to be a template (which is what the NewTemplate argument does if set to True in VBA) but you certainly will be able to save a new document as a template later, using the save as command with its file format parameter set to template, so this does not present any problem. When converting a macro that has NewTemplate:=True, just ignore that in AppleScript and use save as file format format template when saving.

Similarly, the only value for DocumentType other than the default of wdNewBlankDocument (which can just be ignored, since it's the default) that works on the Mac is wdNewWebPage, so, once again, if you should happen to run into DocumentType:= wdNewWebPage, just ignore it and save as file format format HTML when saving. You can also set its view to Online View just after making the document:

     set view type of view of window 1 of newDoc to online view

Making a Document from a Template

Now, the one thing you really need to be able to do is to make a new document from a template, equivalent to the Template argument of the Add Method. Even if it doesn't work in a with properties parameter to make new document (which would be nice) you ought to be able to set the attached template property (which is not read-only) of the new document to any template you wish, like this:

tell application "Microsoft Word"

     set newDoc to make new document

     set attached template of newDoc to "Mac HD:Applications:" & ¬

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

     get name of attached template of newDoc

end tell

Note: when the dictionary says you can provide the "name" of the template, it means the full path. For this article I had to "break up" the path into shorter bits so as not to introduce hard carriage returns in the published version, but you don't need to do this – just enter the whole colon-delimited path of a template. Or type POSIX file "" as alias as Unicode text in Script Editor and drag the file to between the quotes. (Script Debugger makes this even easier by offering a choice of reference types, including "HFS path": just drag the file, with no typing required.)

That will compile and run, and you will even see the correct name (just the name this time: "Test Template.doc") as the result if you include that last line. But nothing changes in the document: no styles, no macros are added. It's a bug in Word 2004. We have been assured that the bug will be fixed in Word 2008 without fail, and that even

make new document with properties {attached template:"Mac HD:" & ¬ "Applications:Microsoft Office 2004:My Templates:Test Template.dot"

will work then.

If it doesn't, there is a workaround suggested by Jonathan West, that works pretty well. After setting the attached template, we insert it using insert file. That gets the text, including headers and footers, the styles, and (in 2004) the macros. All it is missing is the page setup – the margins, line spacing, and so on. If the template uses different settings than your Normal template, including larger header space, for example, you can take the workaround further with more code that opens the template for an instant as a document so as to be able to retrieve its page setup settings.



< 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