applescript word 2004 and templates - one equals two

C

consiglieri

Hi

I have seen the posts concerning applescript and templates when using
word 2004

Following the posts I saw I made the following applescript

tell application "Microsoft Word"

set newDoc to make new document
do Visual Basic "Documents.Add Template:=\"HD:Applications:Microsoft
Office 2004:Templates:User templates:template.dot"




set value of document property "Comments" of active document to
"Applescript works"
save as active document file name "Temp12.doc"
get value of document property "Comments" of active document


end tell

the get value of documetn property can be removed..fills no function.

All of this works well except that when this script is run I have two
documents, one called temp12.doc and one which just an unsaved
document.

Since I am new att applescript as well as vba I was hoping someone
could explain why I am getting two douments here?

Thanks
 
P

Paul Berkowitz

I have seen the posts concerning applescript and templates when using
word 2004

Following the posts I saw I made the following applescript

tell application "Microsoft Word"

set newDoc to make new document
do Visual Basic "Documents.Add Template:=\"HD:Applications:Microsoft
Office 2004:Templates:User templates:template.dot"




set value of document property "Comments" of active document to
"Applescript works"
save as active document file name "Temp12.doc"
get value of document property "Comments" of active document


end tell

the get value of documetn property can be removed..fills no function.

All of this works well except that when this script is run I have two
documents, one called temp12.doc and one which just an unsaved
document.

Since I am new att applescript as well as vba I was hoping someone
could explain why I am getting two douments here?

Because you told it to.

Your first line:

set newDoc to make new document

makes a Blank New Document (and then doesn't ever use the variable you set
to it).

Then your second line:

do Visual Basic "Documents.Add Template:=\"HD:Applications:Microsoft
Office 2004:Templates:User templates:template.dot"

does not attach the template.dot to your newDoc - it makes a NEW document
("Documents.Add ") with template.dot as the attached template. That's the
step that makes the second document and the rest of the script goes on using
this second document and never bothers with the first new document.

So just remove the first line of the script, which you don't need.

(You'd think that another way of doing this would be to set the
AttachedTemplate property of newDoc after making it. Although that does
"attach" the template, it doesn't automatically bring over text, macros etc.
as you might expect, after the fact. There would be ways of doing so, but
not worth the bother. Instead, do the above: just "Add" - i.e. create - a
new document based on the template, and use that.)

The one big problem with having to use 'do Visual Basic' (*see below) is
that it doesn't return a result to AppleScript, so you can't set a varuable
(like newDoc) to the statement. That's why the later lines of the script
keep referring to 'active document': the new document uyou added is
guaranteed to be the active (front) document. You could, howvere, set a
variable to 'active document' in the second line (or later) and use that fr
future lines. That would be necessary if you were going to create other
documents afterwards, for example, and this one would cease to be tnhe front
doc but you still needed to manipulate it before closing it. E.g.:


tell application "Microsoft Word"
do Visual Basic "Documents.Add Template:=\"HD:Applications:Microsoft
Office 2004:Templates:User templates:template.dot\""
set newDoc to active document -- the one you just made
set value of document property "Comments" of newDoc to "Applescript
works"
save as newDoc file name "Temp12.doc"
get value of document property "Comments" of newDoc
end tell


* The reason you are forced to use 'do Visual Basic' here is that the
equivalent AppleScript property 'attached template' isn't working (although
it compiles and does not error). This is a bug, which will definitely be
fixed. (Microsoft has confirmed this a long time ago.) It will need to be
fixed for the next version of Word where 'd Visual Basic' will no longer
work. Assuming that 'do Visual Basic' will error in the next version )I'll
try to make sure that it does so) rather than just do nothing, you could
build this in to your script so it will (should) go on working then:

try
do Visual Basic "Documents.Add Template:=\"HD:Applications:Microsoft
Office 2004:Templates:User templates:template.dot\""
on error
make new document with properties {attached
template:"HD:Applications:Microsoft Office 2004:Templates:User
templates:template.dot"}
end try


But probably it's better just to wait to see what works in the next version.
(Otherwise the above might fail silently.)


By the way, you omitted to include the closing quoted quote \" at the end of
the template's file path, before the regular closing quote. Lucky for you,
VBA fixed this for you, but I wouldn't count on that. Remember, when
including internal quotes inside a literal string (as all 'do Visual Basic'
commands are) you need the backslash before the internal quote.

--
Paul Berkowitz
MVP MacOffice
Entourage FAQ Page: <http://www.entourage.mvps.org/faq/index.html>
AppleScripts for Entourage: <http://macscripter.net/scriptbuilders/>

Please "Reply To Newsgroup" to reply to this message. Emails will be
ignored.

PLEASE always state which version of Microsoft Office you are using -
**2004**, X or 2001. It's often impossible to answer your questions
otherwise.
 
P

Paul Berkowitz

(You'd think that another way of doing this would be to set the
AttachedTemplate property of newDoc after making it. Although that does
"attach" the template, it doesn't automatically bring over text, macros etc.
as you might expect, after the fact. There would be ways of doing so, but not
worth the bother. Instead, do the above: just "Add" - i.e. create - a new
document based on the template, and use that.)

To be more accurate: setting the attached template of an open document (and
this can actually be done by regular AppleScript without recourse to 'do
Visual Basic', by 'set attached template of active document to
"Path:to:the:template.dot" ') will make the template's macros available, but
it will not insert the text of the template into the already-open document.
(You can, however, set the styles of the document to that of the attached
template by following with the simple command 'update styles of active
document'.) To get the text as well as the macros and styles you need to
open a document from the template, which currently (in Word 2004) must be
done by the 'do Visual Basic "Documents.Add Template = ' method.

--
Paul Berkowitz
MVP MacOffice
Entourage FAQ Page: <http://www.entourage.mvps.org/faq/index.html>
AppleScripts for Entourage: <http://macscripter.net/scriptbuilders/>

Please "Reply To Newsgroup" to reply to this message. Emails will be
ignored.

PLEASE always state which version of Microsoft Office you are using -
**2004**, X or 2001. It's often impossible to answer your questions
otherwise.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top