Access.2007.VBA.Bibl..

(John Hannent) #1
Get the path of the current script, extracting it from the ScriptFullNameproperty:

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 DefaultFilePathprop-
erty 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 GetFilemethod; 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 Copymethod of the Fileobject:

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

Part III Adding More Functionality to Office

Free download pdf