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

 
 
Shared Storage Easier

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



 


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