Access.2007.VBA.Bibl..

(John Hannent) #1
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 ScriptFullNameproperty:


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 DefaultFilePathprop-
erty 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 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 Workgroup Templates folder, using the Copymethod of the Fileobject:


fso.CopyFile strCurrentPath, _
strWorkgroupTemplatePath, True
MsgBox strTemplate & “ copied from “ & _
strCurrentPath & “ to “ _
& strWorkgroupTemplatePath
End If

Creating Standalone Scripts with Windows Script Host 17

Free download pdf