Tag Archives: Function

Programming Windows

Changing a Template or Templatepath in Word Documents via Script

During a fileserver migration I encountered a problem – users started to report that it takes unusually long to open Microsoft Word Documents. I did some investigations on this and found out that Word is saving the FQDN of the template path (*.dot) file. You can see this if you open a document in the Word splash screen.

You can manually change the template path within the document options in word – but what if you have thousands of files and you even don’t know which one are affected?

Here’s the answer: I just wrote a little script that will modify all documents within the given path.

You just need to modify the lines 45, 46, 47 to fit your environment

' #####################################################################
' ##
' ## (C) 2012 Michael Miklis (michaelmiklis.de)
' ##
' ## 
' ## Filename:      ChangeWordDocumentTemplate.vbs
' ##
' ## Version:	    1.0
' ##
' ## Release:	    Final 
' ##
' ## Requirements:  -none-
' ## 
' ## Description:   Changes the template in doc files
' ##
' ## This script is provided 'AS-IS'.  The author does not provide 
' ## any guarantee or warranty, stated or implied.  Use at your own 
' ## risk. You are free to reproduce, copy & modify the code, but 
' ## please give the author credit. 
' ##
' ####################################################################
Option Explicit

Dim strFilePath
Dim strPath
Dim intCounter
Dim strFileName
Dim OldServer
Dim NewServer
Dim objDoc
Dim objTemplate
Dim dlgTemplate
Dim objWord
Dim strFileArr
Dim objFs
Dim i

Const wdDialogToolsTemplates = 87

Set objFS = CreateObject("Scripting.Filesystemobject")

Set objWord = CreateObject("Word.Application")
objWord.Visible = false

strFilePath = "C:\Testfiles"
OldServer = "\\server_old\share\folder1\folder1.1" 
NewServer = "\\server_new\share\folder1\folder1.1"


If Right(strFilePath, 1) = "\" Then strFilePath = Left(strFilePath, Len(strFilePath) - 1)

strFileArr = Split(CreateFileList(objFS.GetFolder(strFilePath),true), vbCr)

For i=0 to UBound(strFileArr)
	If NOT strFileArr(i) = "" then
		strFileName = strFileArr(i)

		wscript.echo "--------------------------------------------------"
		wscript.echo "Processing File: " & strFilename

		Set objDoc = objWord.Documents.Open(strFileName)    
		Set objTemplate = objDoc.AttachedTemplate    
		Set dlgTemplate = objWord.Dialogs(wdDialogToolsTemplates)    
		strPath = dlgTemplate.Template

		wscript.echo "Old Template Name: " & strPath

		'// Only process templates based on old server
		If LCase(Left(strPath, Len(OldServer))) = LCase(OldServer) Then
			wscript.echo "Template needs to be changed..."    
			objDoc.AttachedTemplate = NewServer & Mid(strPath, Len(OldServer) + 1)
			wscript.echo "New Template Name: " & NewServer & Mid(strPath, Len(OldServer) + 1)
		End If    

		objDoc.Save                  
		objDoc.Close

		wscript.echo "--------------------------------------------------"
		wscript.echo ""
	End If
Next 

Set objDoc = Nothing
Set objTemplate = Nothing
Set dlgTemplate = Nothing  

'// Close Word
objWord.Quit (False)

'______________________FUNCTIONS & SUB ROUTINES_______________________

Function CreateFileList(objFolder, bRecursive)
	'// <summary> 
	'// 	Creates a List containing all Files
	'// </summary> 
	'// <param name="objFolder">Root-Folder for searching</param> 	
	'// <param name="bRecursive">Search recursive</param>
	Dim objFile, objSubFolder


        For each objFile in objFolder.Files
		If Right(lcase(objFile),4) = ".doc" OR Right(lcase(objFile),5) = ".docx" AND NOT Left(objFile.Name,1) = "~" then
	        	CreateFileList = CreateFileList & objFile.Path & vbCr
		End If
	Next

	If bRecursive = true then
		For each objSubFolder in objFolder.Subfolders
			CreateFileList = CreateFileList & CreateFileList(objSubFolder, true)
	        Next
	End If
End Function

Read More
Featured Programming

VBScript: Include Function

Another interesting VBScript Function I created in the past for larger scripting projects. This will give you the ability to “outsource” code from your main script file. This function is known and built into other languages such as C++, .NET, PHP, etc. for years.

PHP-Include Reference: http://php.net/manual/de/function.include.php
(functionality is pretty much the same in my VBScript function)

' #####################################################################
' ##
' ## (C) 2011 Michael Miklis (michaelmiklis.de)
' ##
' ## 
' ## Filename:      Include.vbs
' ##
' ## Version:	    1.0
' ##
' ## Release:	    Final 
' ##
' ## Requirements:  -none-
' ## 
' ## Description:   Include function for external Code-Files
' ##
' ## This script is provided 'AS-IS'.  The author does not provide 
' ## any guarantee or warranty, stated or implied.  Use at your own 
' ## risk. You are free to reproduce, copy &amp; modify the code, but 
' ## please give the author credit. 
' ##
' ####################################################################
Option Explicit

Include(&quot;C:code.inc.vbs&quot;)

'______________________FUNCTIONS &amp; SUB ROUTINES_______________________

Sub Include(strFilename)
	'// &lt;summary&gt; 
	'// Include function for C-Style include from
	'// external script files
	'// &lt;/summary&gt;
	'// &lt;param name=&quot;strFilename&quot;&gt;external code file&lt;/param&gt; 
	Dim objFSO, objHANDLE


	Set objFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)
	Set objHANDLE = objFSO.OpenTextFile(strFilename)

	ExecuteGlobal objHANDLE.ReadAll
	objHANDLE.Close()

	Set objHANDLE = Nothing
	Set objFSO = Nothing
End Sub


Read More