change Attached Template - Applescript fails

R

Roger Morris

MS Office 11.2.3 and Mac OS 10.4.5

I have many Word documents for which I want to change the attached
template and thought this might be easy to do using Applescript.

This script (testing with just one doc) fails:

----------------------------------
set theTplate to ((choose file with prompt "Template")) as string)
set theDoc to ((choose file with prompt "Document") as string)

tell application "Microsoft Word"
open document file name theDoc
do Visual Basic "ActiveDocument.AttachedTemplate:=\"" & theTplate & "\""
close document theDoc saving yes
end tell
----------------------------------

BUT after the selected document opens:
A message box appears with :
Compile error: / Syntax error

and the VB editor code window contains:

----------------------------------
Sub TmpDDE()
ActiveDocument.AttachedTemplate:="Aragorn:Users:rm:Desktop:aTemplate"
End Sub
----------------------------------
(with 1st line arrowed in LH margin, 2nd line highlighted)

It matters not whether the template is in
~/Desktop or
~/Documents/Microsoft User Data/Templates/My Templates

I believe the "do VB" bit came from a post by Paul Berkowitz although
now I can't find it again so can't give a reference.

Can anyone tell me what is wrong please?

Roger
 
R

Roger Morris

Roger Morris said:
MS Office 11.2.3 and Mac OS 10.4.5

I have many Word documents for which I want to change the attached
template and thought this might be easy to do using Applescript.

This script (testing with just one doc) fails:
----------------------------------
set theTplate to ((choose file with prompt "Template")) as string)
set theDoc to ((choose file with prompt "Document") as string)

tell application "Microsoft Word"
open document file name theDoc
do Visual Basic "ActiveDocument.AttachedTemplate:=\"" & theTplate & "\""
close document theDoc saving yes
end tell
----------------------------------
BUT after the selected document opens:
A message box appears with :
Compile error: / Syntax error
and the VB editor code window contains:
----------------------------------
Sub TmpDDE()
ActiveDocument.AttachedTemplate:="Aragorn:Users:rm:Desktop:aTemplate"
End Sub
---------------------------------- [snip]
I believe the "do VB" bit came from a post by Paul Berkowitz although
now I can't find it again so can't give a reference.

Can anyone tell me what is wrong please?

Roger

Well, I have found the reference. It was in
microsoft.public.mac.office.word
subject: applescript questions
author: Matt Centurion [MSFT]
date: Mon, Nov 21 2005

Matt wrote:
1. Unfortunately the "make new document" in 2004 cannot currently create
a new document based on a template. However you can use "do visual
basic" to do just that:
tell application "Microsoft Word"
set stTemplatePath to ((choose file) as string)
do Visual Basic ("documents.add Template:\"" & stTemplatePath &
"\"")
end tell
2. Do visual baisc is not broken because I just copied the above code
from AppleScript on Word 2004 SP2 where it worked fine. ...

I used ActiveDocument.AttachedTemplate because I want to change the
template in an existing document.

I have now tried Matt's script just as it is, to create a new document
but that doesn't work either. (similar error).

Can I have something else wrong?

Roger
 
P

Paul Berkowitz

You're using the wrong VBA syntax. (Note: there's a bug with the new Word
2004 AppleScript 'attached template' property of 'document' which means you
do have to use the older method of 'do Visual Basic' and VBA code.)

Any time you need to use 'do Visual Basic' you're best off trying it first
as a VBA macro, and consulting the VBA Help, which is very good.

If I wrote that (and I may well have) there's an error there. It should be:


set theTplate to ((choose file with prompt "Template")) as string)
set theDoc to ((choose file with prompt "Document") as string)

tell application "Microsoft Word"
open document file name theDoc
do Visual Basic "ActiveDocument.AttachedTemplate = \"" & theTplate &
"\""
close document theDoc saving yes
end tell


There was a colon there that shouldn't have been there.

On the other hand, in Matt Centurion's code, he left out an "=" sign that
should be there:

tell application "Microsoft Word"
set stTemplatePath to ((choose file) as string)
do Visual Basic ("Documents.Add Template:=\"" & stTemplatePath & "\"")
end tell


We both had an error. When fixed, as above, my script opens an existing
document and attaches the template, then closes the document; Matt's creates
a new document and attaches the template.

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

John McGhie [MVP - Word and Word Macintosh]

Hi Roger:

This entire mechanism is extremely flaky, as I am sure you are well aware.

The two places I would suspect are "ActiveDocument" and "Path separator".

VBA generally does not like a colon as a path separator. There's a VB
constant for it - PathSeparator that I usually use to ensure that VBA
actually recognises the character.

Word 2004 security will have unpredictable effects with templates on the
desktop. For Word to know that it's a template and it's "safe", it MUST by
the in the "My Templates" folder in the user's "User Templates" path (query
Word for it).

However, I suspect that your main problem may be with "ActiveDocument".
Just don't use this if you are working cross-application or cross-template.
Your scope will be changing all the time.

Set an Object equal to the document, then refer to the Object. This ensures
your code does not pass out of scope at a critical moment. Don't forget to
set your object to "Nothing" before closing each document, otherwise you
will eventually run out of memory and blow up.

Also: I note that you do not have a file extension on your template
(".dot"). Word requires the extension to be specified if it exists, and not
if it doesn't. In other words, if your template is MyTemplate.dot you must
refer to it as "MyTemplate.dot". "MyTemplate" will fail. And vice versa.

Lastly, are you sure the file in question *is* a template? An attempt to
attach a document to another document will fail. If you open your template
and do a File>Save As, Word should automatically point you to the My
Templates folder. If it doesn't, the file is not a template, regardless of
what the extension says, and you can't attach it.

Other than that, damned if I can see anything wrong :)

Cheers


Roger Morris said:
MS Office 11.2.3 and Mac OS 10.4.5

I have many Word documents for which I want to change the attached
template and thought this might be easy to do using Applescript.

This script (testing with just one doc) fails:
----------------------------------
set theTplate to ((choose file with prompt "Template")) as string)
set theDoc to ((choose file with prompt "Document") as string)

tell application "Microsoft Word"
open document file name theDoc
do Visual Basic "ActiveDocument.AttachedTemplate:=\"" & theTplate & "\""
close document theDoc saving yes
end tell
----------------------------------
BUT after the selected document opens:
A message box appears with :
Compile error: / Syntax error
and the VB editor code window contains:
----------------------------------
Sub TmpDDE()
ActiveDocument.AttachedTemplate:="Aragorn:Users:rm:Desktop:aTemplate"
End Sub
---------------------------------- [snip]
I believe the "do VB" bit came from a post by Paul Berkowitz although
now I can't find it again so can't give a reference.

Can anyone tell me what is wrong please?

Roger

Well, I have found the reference. It was in
microsoft.public.mac.office.word
subject: applescript questions
author: Matt Centurion [MSFT]
date: Mon, Nov 21 2005

Matt wrote:
1. Unfortunately the "make new document" in 2004 cannot currently create
a new document based on a template. However you can use "do visual
basic" to do just that:
tell application "Microsoft Word"
set stTemplatePath to ((choose file) as string)
do Visual Basic ("documents.add Template:\"" & stTemplatePath &
"\"")
end tell
2. Do visual baisc is not broken because I just copied the above code
from AppleScript on Word 2004 SP2 where it worked fine. ...

I used ActiveDocument.AttachedTemplate because I want to change the
template in an existing document.

I have now tried Matt's script just as it is, to create a new document
but that doesn't work either. (similar error).

Can I have something else wrong?

Roger

--

Please reply to the newsgroup to maintain the thread. Please do not email
me unless I ask you to.

John McGhie <[email protected]>
Microsoft MVP, Word and Word for Macintosh. Consultant Technical Writer
Sydney, Australia +61 (0) 4 1209 1410
 
R

Roger Morris

Paul Berkowitz said:
You're using the wrong VBA syntax. (Note: there's a bug with the new Word
2004 AppleScript 'attached template' property of 'document' which means you
do have to use the older method of 'do Visual Basic' and VBA code.)

Any time you need to use 'do Visual Basic' you're best off trying it first
as a VBA macro, and consulting the VBA Help, which is very good.

If I wrote that (and I may well have) there's an error there. It should be:


set theTplate to ((choose file with prompt "Template")) as string)
set theDoc to ((choose file with prompt "Document") as string)

tell application "Microsoft Word"
open document file name theDoc
do Visual Basic "ActiveDocument.AttachedTemplate = \"" & theTplate &
"\""
close document theDoc saving yes
end tell


There was a colon there that shouldn't have been there.

On the other hand, in Matt Centurion's code, he left out an "=" sign that
should be there:

tell application "Microsoft Word"
set stTemplatePath to ((choose file) as string)
do Visual Basic ("Documents.Add Template:=\"" & stTemplatePath & "\"")
end tell


We both had an error. When fixed, as above, my script opens an existing
document and attaches the template, then closes the document; Matt's creates
a new document and attaches the template.

Thank you Paul.
Using your examples above I now have both versions working.
I think I must have deleted Matt's "=". It was in his original post.

I may also have suffered from pitfalls pointed out by John McGhie in his
response.

I find VB syntax confusing - shall have to look up why "=" in one case
and ":=" in the other!

Thanks to both for much much appreciated help.

Roger
 
R

Roger Morris

Hi John,

Thank you for those tips.

I can't back-track very much now but it's possible I had a combination
of syntax errors and 'flaky' usages.

Thanks to your help and the correction of syntax provided by Paul I now
have both versions of the script working.

Roger


John McGhie said:
Hi Roger:

This entire mechanism is extremely flaky, as I am sure you are well aware.

The two places I would suspect are "ActiveDocument" and "Path separator".

VBA generally does not like a colon as a path separator. There's a VB
constant for it - PathSeparator that I usually use to ensure that VBA
actually recognises the character.

Word 2004 security will have unpredictable effects with templates on the
desktop. For Word to know that it's a template and it's "safe", it MUST by
the in the "My Templates" folder in the user's "User Templates" path (query
Word for it).

However, I suspect that your main problem may be with "ActiveDocument".
Just don't use this if you are working cross-application or cross-template.
Your scope will be changing all the time.

Set an Object equal to the document, then refer to the Object. This ensures
your code does not pass out of scope at a critical moment. Don't forget to
set your object to "Nothing" before closing each document, otherwise you
will eventually run out of memory and blow up.

Also: I note that you do not have a file extension on your template
(".dot"). Word requires the extension to be specified if it exists, and not
if it doesn't. In other words, if your template is MyTemplate.dot you must
refer to it as "MyTemplate.dot". "MyTemplate" will fail. And vice versa.

Lastly, are you sure the file in question *is* a template? An attempt to
attach a document to another document will fail. If you open your template
and do a File>Save As, Word should automatically point you to the My
Templates folder. If it doesn't, the file is not a template, regardless of
what the extension says, and you can't attach it.

Other than that, damned if I can see anything wrong :)

Cheers

[snip]
 
J

John McGhie [MVP - Word and Word Macintosh]

Hi Roger:

I find VB syntax confusing - shall have to look up why "=" in one case
and ":=" in the other!

If *I* ever find out, I'll tell you :)

The = sign means just that. The := means "becomes".

If a method or function returns a result, you must enclose its arguments in
parentheses. If you use the parentheses, you must use := to assign the
returned value to a variable.

If you don't need the returned value, or the method doesn't return a value,
you don't need the variable, so you don't need the parentheses or the
"becomes", just = will do.

I get it wrong about half the time :)

--

Please reply to the newsgroup to maintain the thread. Please do not email
me unless I ask you to.

John McGhie <[email protected]>
Microsoft MVP, Word and Word for Macintosh. Consultant Technical Writer
Sydney, Australia +61 (0) 4 1209 1410
 

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