open new document based on template (Applescript)

P

Possum Stu

I hope this is an easy one. I have a template called bluetemplate.dot.

In Applescript, to create a blank new Word document based on the Normal
template, I use:

set myDocument to make new document

or simply

make new document

Basically I want to replicate the experience of opening a new document
from a template in the templates folder. How do I create a blank new
Word document based on a template of my choice (bluetemplate.dot) so
that the stored text, styles, and fields open in a new unnamed
document?
 
D

Daiya Mitchell

Assuming you are doing this in Word, a pretty easy way would be to record it
as a macro. While recording, use File | Project Gallery, select the
template, click Open. Once the macro is created, use Tools | Customize to
give it a keyboard shortcut or put it on a toolbar.

If you really want Applescript, I haven't a clue, but someone else will
probably come along and tell you. It could be handy to have a clickable
script on a desktop to open up a new doc based on a certain template.
 
P

Paul Berkowitz

I hope this is an easy one. I have a template called bluetemplate.dot.

In Applescript, to create a blank new Word document based on the Normal
template, I use:

set myDocument to make new document

or simply

make new document

Basically I want to replicate the experience of opening a new document
from a template in the templates folder. How do I create a blank new
Word document based on a template of my choice (bluetemplate.dot) so
that the stored text, styles, and fields open in a new unnamed
document?

We just went through this a few days ago.

There's a bug in Word AppleScript. What you _should_ be able to do, but it
doesn't work, is

make new document with properties {attached
template:"path:to:template:here.dot"}

So you have to do it the "old" way, using 'do Visual Basic':

do Visual Basic "Documents.Add Template:=\"path:to:template:here.dot\""


As Matt Centurion of MacBU pointed out, that will work as long as the
template is available somewhere locally. I.e., it will work perfectly on
your own computer. If you plan to distribute the document to others, the
document will have whatever text and formatting you have in the template,
but it won't have the macros, auto-text entries and other 'special" features
unless you also distribute the template and code the path in such a way as
will work on other computers (which is possible to do if you can trust the
users to put it in the right place) or use Organizer to transfer the macros,
etc. from template to document - which can also be done by VBA and
(probably) regular AppleScript.



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

Possum Stu

Thank you for your helpful response.

Your concerns about users placing the templates in the right place are
well-founded. I'm attempting to get around this by furnishing not only
an updated template, but a script bundle that includes the new template
as one of its components. The Applescript I'm using is for this part
is:

set myPath to path to me as string
set newTemplate to (myPath &
"Contents:Resources:templateName_rev3.dot")
open newTemplate

Now that the proper template is open, I have to script a way to save it
to the right directory and we'll be set.
 
J

John McGhie [MVP - Word and Word Macintosh]

Hi Poss:

OK, let me give you some ideas. The trick is to make your code go find the
template. VBA/AppleScript contain a set of properties you can retrieve that
lets you know where things like the active document, the attached template,
the template folder and the startup path are. Here's some code that goes
and finds the template it wants:

Sub UpdateStyles()
Const Message As String = "Do you want to update all document styles?"
Dim WkrsCompTemplate As String
Dim doIt As Long

WkrsCompTemplate = Options.DefaultFilePath(wdStartupPath) & "\" &
TemplateName

' Updates styles

Application.OrganizerCopy _
Source:=WkrsCompTemplate, _
Destination:=ActiveDocument.FullName, _
Name:="Question", _
Object:=wdOrganizerObjectStyles

If ActiveDocument.Name <> TemplateName Then
doIt = MsgBox(Message, vbOKCancel, "Attach Template Macro")
If doIt = 1 Then

With ActiveDocument
.AttachedTemplate = WkrsCompTemplate
.UpdateStyles
.AttachedTemplate = Application.NormalTemplate
End With

End If
End If
End Sub

This macro updates a documents styles then attaches Normal template (to
prevent any further updates).

The active ingredient is Options.DefaultFilePath(wdStartupPath) which
returns the path to the user¹s Word:Startup folder as a string. There are
various options: WdDefaultFilePath can be one of these WdDefaultFilePath
constants.
wdAutoRecoverPath
wdCurrentFolderPath
wdGraphicsFiltersPath
wdProgramPath
wdStartupPath
wdTempFilePath
wdToolsPath
wdUserOptionsPath
wdWorkgroupTemplatesPath
wdBorderArtPath
wdDocumentsPath
wdPicturesPath
wdProofingToolsPath
wdStyleGalleryPath
wdTextConvertersPath
wdTutorialPath
wdUserTemplatesPath

This means all you have to do is tell the user to put your template in their
³User Templates Folder². You can even make a macro to do it for them, with
a button that says ³Click here to install² :)

The variable TemplateName has been pre-loaded with the name of the template
we want (just the name, without the path) as a string. I normally have a
separate module that contains nothing but constants, to enable me to quickly
re-purpose macros for different customers. That one is defined as:

Public Const TemplateName As String = "Tender Work in Progress v5.dot"

The keyword ³Public² makes it persistent and available to any macro anywhere
in the same template.

Sorry, I can¹t do this stuff in AppleScript. But Paul can, and will be very
helpful if you ask nicely :)

Note that the last time Paul and I tried to get this stuff working in
AppleScript, we discovered some of it doesn¹t work right in AppleScript.
And some of those constants I gave you in the list above will not be
available on the Mac, or will not work properly, because I pinched the list
from Word 2003 Help.

Hope this helps


Thank you for your helpful response.

Your concerns about users placing the templates in the right place are
well-founded. I'm attempting to get around this by furnishing not only
an updated template, but a script bundle that includes the new template
as one of its components. The Applescript I'm using is for this part
is:

set myPath to path to me as string
set newTemplate to (myPath &
"Contents:Resources:templateName_rev3.dot")
open newTemplate

Now that the proper template is open, I have to script a way to save it
to the right directory and we'll be set.

--

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