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