Setting the User Templates and Workgroup Templates Paths

o view (or set) the User Templates and Workgroup Templates path, click the Office button in any Word document and click the Word Options button:

iO«*"-1

Dpi

Home 1

Insert

Paget ayout

Reference?

Mailings H

-1

Recent Documents gp a». Save

Recent Documents

1 Microsoft Office Integration Schedule v 6,doc

2 Access Arch on Column 113 — Avoiding the Obj... "t2!

3 MicrosoftOfficeIntegrationSchedulev5.doc t^

4 frmSendAndSave Form Module.doc

5 Access Archon Column 113 — Avoiding the Obj... W 5 AccessArchonArticles.doc

7 Access Archon Column 123 — Saving EMail Atta... i-1

8 M&TBankmail deposit envelope,doc

9 Chapter 17 (Exporting) AR3.doc

Opening the Word Options dialog.

On the Word Options dialog, select the Advanced page, scroll down to the bottom of the page, and click the File Locations button:

VUoid Options

mm

Pe,S„„,tee

□ Prompt before

aving Normal template i)

-

Display

□ Always create

ackup copy

O Copy remotely

tored files onto your computer, and update the remote file when saving

Proofing

12] Allow background saves

Customization

¡0 Save smart tag

as XML properties in Web pages

0 Save form data

as delimited tesctfile

Add-In s

IS Embed linguist

c data

Trust Center

Embed smart tags

Resources

Genei.il

IÎ/1 Provide feed back with so und

Iffl Provide feedback with animation

O Confirm file fo

mat conversion on open

H] Update autom

tic links at open

□ Allow opening

a document in Draft view

O Allow backgro

nd open of web pages

H Enablebackgrc

Und «pagination

□ Show add-in u

er interface errors

Mailing address:

55 Main Street

Somewhere NH 2(1283

-

fUIM.

1 Web Options... | [ Service Options...

Compatibility options foi: i®] Document! 1

Lay out this docum

ent as If created In: | Microsoft Office Ward 2007 (Beta) | *r |

S Layout Options

1 OK || Cancel

The Advanced page of the Word Options dialog.

The File Locations dialog opens, where you can view and set the User Templates and/or Workgroup Templates folder paths. To set a folder path, simply select the appropriate selection (User templates or Workgroup templates) in the File types list, click the Modify button, and browse to the desired folder location:

File Loc.itioiis ES|g

Fife Lacatiöns types: Location:

Documents

D:\Documents

Clipart pictures

User templates

D:\Templates

Workaroup templates

AutoRecover files

C:\,. .\Roaming\Microsoft\Word

Tools

C:\,..\Microsoft Office\Officel2

Startup

C:\,.. \Microsof t\Word\5T ARTUP

Modify.,. 1

The deFault Initiai}s treated as a trusted fBKbpSQing filas. Ifyôii ijïarng^çfJÉicationj make sure that thé ne^tiq^tfefiï^ièoJr'e.

The File Locations dialog, where you can set the template paths.

To test one of these scripts, copy the Test.dot template to the same folder as the script (you can also copy any Word template, and rename it "Test.dot.") Right-click the CopyTemplateUser.vbs file in an Explorer window and select "Microsoft (r) Windows Based Script Host" from the Open With menu, as shown in Figure 17.12.

FIGURE 17.12

Running a WSH script.

FIGURE 17.12

Running a WSH script.

If you have not commented out this message box, you will first see a message listing the template name and current path (Figure 17.13).

FIGURE 17.13

A message box listing the template name and path for copying a Word template.

FIGURE 17.13

Next, if the copy is successful, you will get the message (the path may be different on your system) shown in Figure 17.14.

A success message after copying the template to the User Templates path.

If the copy could not be done, because Test.dot could not be located in the current folder, you will instead get the message shown in Figure 17.15.

FIGURE 17.15

A message indicating that the template to be copied could not be found.

FIGURE 17.15

When you open the MSE, you may see a script open in the code pane, and other scripts listed in the Project Explorer; these are the scripts that were recently opened. If you see the script you want to edit in the Project Explorer, just select it.

The text of CopyTemplateUser.vbs is listed next. There are several commented-out MsgBox statements in this script; if you want to display these message boxes, uncomment these lines by removing the apostrophe at the beginning of the line:

Dim strTemplate Dim strUserTemplatePath Dim appWord Dim fso

Dim strScriptPath

Dim strScriptName

Dim strScriptNameAndPath

Dim fil

Dim strPrompt

Dim strTemplatePath strScriptName = WScript.ScriptName

Get the path of the current script, extracting it from the ScriptFullName property:

strScriptNameAndPath = WScript.ScriptFullName strScriptPath = Mid(strScriptNameAndPath, 1, _

Len(strScriptNameAndPath) - Len(strScriptName)) strTemplate = "Test.dot"

Get the User Templates path from the Word Options dialog, using the DefaultFilePath property with the argument 2 (most named constants can't be used in VBS):

Set appWord = WScript.CreateObject("Word.Application")

strUserTemplatePath = appWord.Options.DefaultFilePath(2) _ & "\"

'MsgBox "User templates path: " & strUserTemplatePath If strUserTemplatePath <> "\" Then Set fso = _

WScript.CreateObject("Scripting.FileSystemObject") strTemplatePath = strScriptPath & strTemplate 'MsgBox "Source template and path: " & strTemplatePath

Try to locate the template file, using the FileSystemObject's GetFile method; quit if it is not found:

Set fil = fso.GetFile(strTemplatePath) If fil Is Nothing Then strPrompt = "Can't find " & strTemplate & _

" in " & strScriptPath & " folder; canceling" MsgBox strPrompt, vbCritical + vbOKOnly Quit Else

Copy the template to the User Templates folder, using the Copy method of the File object:

fil.Copy strUserTemplatePath, True MsgBox strTemplate & " copied to " _ & strUserTemplatePath

End If Else strPrompt = _

"User template path not selected; canceling" MsgBox strPrompt, vbCritical + vbOKOnly End If

The CopyTemplateWorkgroup.vbs is similar, but it has some more error handling, to deal with the situation where the Workgroup Templates path has not been selected in Word:

Dim strTemplate Dim strWorkgroupTemplatePath Dim appWord Dim fso

Dim strScriptPath

Dim strScriptName

Dim strScriptNameAndPath

Dim fil

Dim strPrompt

Dim strTemplatePath

Dim strCurrentPath strScriptName = WScript.ScriptName

Get the path of the current script, extracting it from the ScriptFullName property:

strScriptNameAndPath = WScript.ScriptFullName strScriptPath = Mid(strScriptNameAndPath, 1, _

Len(strScriptNameAndPath) - Len(strScriptName)) strTemplate = "Test.dot"

strCurrentPath = strScriptPath & strTemplate

Get the User Templates path from the Word Options dialog, using the DefaultFilePath property with the argument 2 (most named constants can't be used in VBS):

Set appWord = WScript.CreateObject("Word.Application") strWorkgroupTemplatePath = _

appWord.Options.DefaultFilePath(3) & "\" If strWorkgroupTemplatePath <> "\" Then Set fso = _

WScript.CreateObject("Scripting.FileSystemObject") strTemplatePath = strScriptPath & strTemplate 'MsgBox "Source template and path: " & strTemplatePath

Try to locate the template file, using the FileSystemObject's GetFile method; quit if it is not found:

Set fil = fso.GetFile(strTemplatePath) If fil Is Nothing Then strPrompt = "Can't find " & strTemplate & _

" in " & strScriptPath & " folder; canceling" MsgBox strPrompt, vbCritical + vbOKOnly Quit Else

Copy the template to the Workgroup Templates folder, using the Copy method of the File object:

fso.CopyFile strCurrentPath, _

strWorkgroupTemplatePath, True MsgBox strTemplate & " copied from " & _ strCurrentPath & " to " _

& strWorkgroupTemplatePath

End If

Else strPrompt = _

"Workgroup template path not selected; canceling" MsgBox strPrompt, vbCritical + vbOKOnly End If

One or the other of these files (with the appropriate template name replacing "Test.dot") would be useful as part of an Office application that includes one or more Word templates; all the user needs to do to install the template(s) in the right location is to run the script.

If you need to copy a set of templates to the User Templates or Workgroup Templates folder, you don't need to hard-code the template names, so long as the templates you want to copy are the only templates in the current folder. The CopyAllTemplates.vbs script listed next copies all the Word 2007 (*.dotx) templates in the current folder to the User Templates folder:

Dim strTemplate Dim strUserTemplatePath Dim appWord Dim fso

Dim strScriptPath Dim strScriptName Dim strScriptNameAndPath Dim fil

Dim filTemplate

Dim strPrompt

Dim strTemplatePath strScriptName = WScript.ScriptName strScriptNameAndPath = WScript.ScriptFullName

Get the path of the current script:

strScriptPath = Mid(strScriptNameAndPath, 1, _

Len(strScriptNameAndPath) - Len(strScriptName))

Get the User Templates path from the Word Options dialog, using the DefaultFilePath property with the 2 argument:

Set appWord = WScript.CreateObject("Word.Application")

strUserTemplatePath = appWord.Options.DefaultFilePath(2) _ & "\"

'MsgBox "User templates path: " & strUserTemplatePath If strUserTemplatePath <> "\" Then Set fso = _

WScript.CreateObject("Scripting.FileSystemObject") Set fld = fso.GetFolder(strScriptPath)

Copy all Word 2007 (*.dotx) templates from the current folder to User Templates folder, with a message box for each one (comment out the MsgBox line to suppress these messages):

lngCount = 0

For Each fil In fld.Files

If Right(fil.Name, 4) = "dotx" Then strTemplate = fil.Name fil.Copy strUserTemplatePath, True MsgBox strTemplate & " copied to " _

& strUserTemplatePath lngCount = lngCount + 1 End If Next Else strPrompt = _

"User template path not selected; canceling" MsgBox strPrompt, vbCritical + vbOKOnly End If noti

The sample database for this chapter is Northwind.accdb. This is a version of the sample Northwind database, with its tables renamed according to the Leszynski Naming Convention.

0 0

Post a comment