Welcome, Register for free! or Login below:
EduGeek.net RSS Feeds Register FAQ Members Social Groups User Map Calendar Search Today's Posts Mark Forums Read

Scripts If you need or have any scripts then get 'em here.

Go Back   EduGeek.net Forums > Coding and Web Development > Scripts
Reply
 
LinkBack Thread Tools Search Thread
Sponsored Links
Old 12-05-2009, 05:25 PM   #1
 
jonwitts's Avatar
 
Join Date: Dec 2006
Location: England
Posts: 5
uk
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0 jonwitts is an unknown quantity at this point
Default VBA Script to change word document template location

HI,

We recently moved our Workgroup Templates for Word to a new file server in anticipation of shutting down the old file server for good. We successfully changed the loaction of the Workgroup templates in Microsoft Office with GP, and replicated the file share to the new server. We then shut the server down for good.

Now comes the fun part! All of the documents that were created from any of the workgroup templates on the old server still reference the old server in the document template setting. This is slowing the loading time of these documents right down and casuing masses of uneccessary network traffic.

I have read this VBA Macro script on Microsoft's site ( Documents that have attached templates take a long time to open in Word 2002 and in Word 2003 ), method 3; but can not get it to work...

Can anyone see where I am going wrong?

Cheers

Jon

------------------------------------------

Sub rename_temp_dir()
Dim strFilePath As String
Dim strPath As String
Dim intCounter As Integer
Dim strFileName As String
Dim OldServer As String
Dim NewServer As String
Dim objDoc As Document
Dim objTemplate As Template
Dim dlgTemplate As Dialog

OldServer = "<\\server>"
NewServer = "<\\south_hunsley>"
strFilePath = InputBox("What is the folder location that you want to use?")
If Right(strFilePath, 1) <> "\" Then strFilePath = strFilePath & "\"
strFileName = Dir(strFilePath & "*.doc")
Do While strFileName <> ""
Set objDoc = Documents.Open(strFilePath & strFileName)
Set objTemplate = objDoc.AttachedTemplate
Set dlgTemplate = Dialogs(wdDialogToolsTemplates)
strPath = dlgTemplate.Template
If LCase(Left(strPath, 8)) = LCase(OldServer) Then
objDoc.AttachedTemplate = NewServer & Mid(strPath, 9)
End If
strFileName = Dir()
objDoc.Save
objDoc.Close
Loop
Set objDoc = Nothing
Set objTemplate = Nothing
Set dlgTemplate = Nothing

End Sub
  Reply With Quote
Old 13-05-2009, 12:36 PM   #2
 
jonwitts's Avatar
 
Join Date: Dec 2006
Location: England
Posts: 5
uk
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0 jonwitts is an unknown quantity at this point
Default

OK... Found it myself this morning...

It was the < > at the start and end of both server names that was throwing it...

Here is the working version...

Now all I need to do is get it to read through sub folders too...

---------------

Sub rename_temp_dir()
Dim strFilePath As String
Dim strPath As String
Dim intCounter As Integer
Dim strFileName As String
Dim OldServer As String
Dim NewServer As String
Dim objDoc As Document
Dim objTemplate As Template
Dim dlgTemplate As Dialog

OldServer = "\\b091-jonw\templates"
NewServer = "\\south_hunsley\templates"
strFilePath = InputBox("What is the folder location that you want to use?")
If Right(strFilePath, 1) <> "\" Then strFilePath = strFilePath & "\"
strFileName = Dir(strFilePath & "*.doc")
Do While strFileName <> ""
Set objDoc = Documents.Open(strFilePath & strFileName)
Set objTemplate = objDoc.AttachedTemplate
Set dlgTemplate = Dialogs(wdDialogToolsTemplates)
strPath = dlgTemplate.Template
If LCase(Left(strPath, 21)) = LCase(OldServer) Then
objDoc.AttachedTemplate = NewServer & Mid(strPath, 22)
End If
strFileName = Dir()
objDoc.Save
objDoc.Close

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

End Sub
  Reply With Quote
Old 13-05-2009, 02:55 PM   #3
 
jonwitts's Avatar
 
Join Date: Dec 2006
Location: England
Posts: 5
uk
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0 jonwitts is an unknown quantity at this point
Default [SOLVED] - change Word Document Template recursively

Just in case anyone else needs this at some point; here is the working Macro to recursively change the workgroup template server setting in Word documents...

---------------------------------------------------------------------------

Sub recursive_rename_temp_dir()

On Error Resume Next

Dim colFiles As New Collection
Dim strFilePath As String
Dim strFileType As String
Dim strFileName As String
Dim OldServer As String
Dim NewServer As String
Dim objDoc As Document
Dim objTemplate As Template
Dim dlgTemplate As Dialog

' Set the name of old and new server here
OldServer = "\\server\templates"
NewServer = "\\south_hunsley\templates"

'Message prompt for folder location
strFilePath = InputBox("What is the folder location that you want to use?")

'Message prompt for file type
strFileType = InputBox("What is the file extension you are looking for, including the dot?")

RecursiveDir colFiles, strFilePath, "*" & strFileType, True

Dim vFile As Variant
For Each vFile In colFiles
Debug.Print vFile

'vFile returns full file path - split it at the last "\" to get file name
strFilePath = vFile

SeparatePathAndFile strFilePath, strFileName

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

'first number equals number of characters in OldServer string, second number is incremented by one
If LCase(Left(strPath, 18)) = LCase(OldServer) Then
objDoc.AttachedTemplate = NewServer & Mid(strPath, 19)
End If

objDoc.Save
objDoc.Close

Next vFile

End Sub

Private Sub SeparatePathAndFile(ByRef io_strPath As String, ByRef o_strFileName As String)
'io_strPath - Input/output parameter containing the entire path with file name
' - Will Return the path only
'o_strFileName - Output parameter that will contain the name of the File

Dim strPath() As String
Dim lngIndex As Long

strPath() = Split(io_strPath, "\") 'Put the Parts of our path into an array
lngIndex = UBound(strPath)
o_strFileName = strPath(lngIndex) 'Get the File Name from our array
strPath(lngIndex) = "" 'Remove the File Name from our array
io_strPath = Join(strPath, "\") 'Rebuild our path from our array

End Sub

Public Function RecursiveDir(colFiles As Collection, _
strFolder As String, _
strFileSpec As String, _
bIncludeSubfolders As Boolean)

Dim strTemp As String
Dim colFolders As New Collection
Dim vFolderName As Variant

'Add files in strFolder matching strFileSpec to colFiles
strFolder = TrailingSlash(strFolder)
strTemp = Dir(strFolder & strFileSpec)
Do While strTemp <> vbNullString
colFiles.Add strFolder & strTemp
strTemp = Dir
Loop

If bIncludeSubfolders Then
'Fill colFolders with list of subdirectories of strFolder
strTemp = Dir(strFolder, vbDirectory)
Do While strTemp <> vbNullString
If (strTemp <> ".") And (strTemp <> "..") Then
If (GetAttr(strFolder & strTemp) And vbDirectory) <> 0 Then
colFolders.Add strTemp
End If
End If
strTemp = Dir
Loop

'Call RecursiveDir for each subfolder in colFolders
For Each vFolderName In colFolders
Call RecursiveDir(colFiles, strFolder & vFolderName, strFileSpec, True)
Next vFolderName
End If

End Function


Public Function TrailingSlash(strFolder As String) As String
If Len(strFolder) > 0 Then
If Right(strFolder, 1) = "\" Then
TrailingSlash = strFolder
Else
TrailingSlash = strFolder & "\"
End If
End If
End Function
  Reply With Quote
Reply

EduGeek.net Forums > Coding and Web Development > Scripts

Similar Threads
Thread Thread Starter Forum Replies Last Post
[MS Office - 2007] password protect a word document RabbieBurns Office Software 9 21-03-2009 11:01 AM
[MS Office - 2003] Word document recovery Griffo Office Software 1 07-01-2009 01:44 PM
[MS Office - 2003] Printing Serial number on word document. maniac Office Software 1 26-09-2008 02:37 PM
Error trying to change link location rush_tech EduGeek Joomla 1.5 Package 1 20-05-2008 10:19 PM
change the default save as location. ozzy How do you do....it? 4 06-12-2007 03:36 PM



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search Thread
Search Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 06:47 PM.
Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.
Copyright EduGeek.net




website uptime

© 2005 - 2009 EduGeek.net
SERVER: 4
no new posts