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