+ Reply to Thread
Results 1 to 3 of 3

Thread: VBA Script to change word document template location

  Share/Bookmark
  1. #1

    Reputation
    jonwitts's Avatar
    Join Date
    Dec 2006
    Location
    England
    Posts
    12
    Thank Post
    4
    Thanked 1 Time in 1 Post
    Rep Power
    0

    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

  2. #2

    Reputation
    jonwitts's Avatar
    Join Date
    Dec 2006
    Location
    England
    Posts
    12
    Thank Post
    4
    Thanked 1 Time in 1 Post
    Rep Power
    0

    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

  3. #3

    Reputation
    jonwitts's Avatar
    Join Date
    Dec 2006
    Location
    England
    Posts
    12
    Thank Post
    4
    Thanked 1 Time in 1 Post
    Rep Power
    0

    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 to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. password protect a word document
    By RabbieBurns in forum Office Software
    Replies: 9
    Last Post: 21-03-2009, 10:01 AM
  2. Word document recovery
    By Griffo in forum Office Software
    Replies: 1
    Last Post: 07-01-2009, 12:44 PM
  3. Printing Serial number on word document.
    By maniac in forum Office Software
    Replies: 1
    Last Post: 26-09-2008, 02:37 PM
  4. Error trying to change link location
    By rush_tech in forum EduGeek Joomla 1.5 Package
    Replies: 1
    Last Post: 20-05-2008, 10:19 PM
  5. change the default save as location.
    By ozzy in forum How do you do....it?
    Replies: 4
    Last Post: 06-12-2007, 02:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts