![]() |
#12
|
||||
|
||||
![]()
When templates other than Word’s Normal template are used to create a document, the template’s path & name are stored with the document. If that path is a network path, a change to the server name will break the link. As you've observed, the result can be significant delays in opening the documents on the new server. See: http://support.microsoft.com/?kbid=830561. The same effect occurs when the file is opened on a computer attached to a different network.
The following macro can be used to update the template paths or, if a new template path can’t be found, to point it to Word’s Normal template. Code is included to restore the original date/time stamps of the updated files. In the code, simply replace however much of the old & new template paths differ in the OldServer and NewServer variables. Code:
Option Explicit Dim FSO As Object 'a FileSystemObject Dim oFolder As Object 'the folder object Dim oSubFolder As Object 'the subfolders collection Dim oFiles As Object 'the files object Dim i As Long, j As Long Sub Main() ' Minimise screen flickering Application.ScreenUpdating = False Dim StrFolder As String ' Browse for the starting folder StrFolder = GetTopFolder If StrFolder = "" Then Exit Sub i = 0: j = 0 ' Search the top-level folder Call GetFolder(StrFolder & "\") ' Search the subfolders for more files Call SearchSubFolders(StrFolder) ' Return control of status bar to Word Application.StatusBar = "" ' Restore screen updating Application.ScreenUpdating = True MsgBox i & " of " & j & " files updated.", vbOKOnly End Sub Function GetTopFolder() As String GetTopFolder = "" Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0) If (Not oFolder Is Nothing) Then GetTopFolder = oFolder.Items.Item.Path Set oFolder = Nothing End Function Sub SearchSubFolders(strStartPath As String) If FSO Is Nothing Then Set FSO = CreateObject("Scripting.FileSystemObject") Set oFolder = FSO.GetFolder(strStartPath) Set oSubFolder = oFolder.subfolders For Each oFolder In oSubFolder Set oFiles = oFolder.Files ' Search the current folder Call GetFolder(oFolder.Path & "\") ' Call ourself to see if there are subfolders below Call SearchSubFolders(oFolder.Path) Next Set FSO = Nothing End Sub Sub GetFolder(StrFolder As String) Dim strFile As String strFile = Dir(StrFolder & "*.doc") ' Process the files in the folder While strFile <> "" ' Update the status bar is just to let us know where we are Application.StatusBar = StrFolder & strFile Call UpdateTemplateRefs(StrFolder & strFile) strFile = Dir() Wend End Sub Sub UpdateTemplateRefs(strDoc As String) ' This sub updates the template paths for files after a server ' change. Simply insert however much of the lower end of the ' server paths differ as the OldServer and NewServer variables. Dim OldServer As String, NewServer As String, strPath As String Dim oItem As Object, StrDtTm As String OldServer = "\\TSB\VOL1": NewServer = "\\TSLSERVER\Files" ' Store the file's current Date/Time stamp. If FSO Is Nothing Then Set FSO = CreateObject("Scripting.FileSystemObject") Set oItem = FSO.GetFile(strDoc) StrDtTm = oItem.DateLastModified ' Open the document Documents.Open strDoc, AddToRecentFiles:=False, ReadOnly:=False, Format:=wdOpenFormatAuto With ActiveDocument If .ProtectionType = wdNoProtection Then ' Update the template path strPath = Dialogs(wdDialogToolsTemplates).Template If LCase(Left(strPath, Len(OldServer))) = LCase(OldServer) Then ' Update the file counter for changed files i = i + 1 ' Get the new template path & name strPath = NewServer & Mid(strPath, Len(OldServer) + 1) ' Check whether the template exists If Dir(strPath) <> "" Then ' If found, update the path .AttachedTemplate = NewServer & Mid(strPath, Len(OldServer) + 1) Else ' If not found, reset the template to 'Normal' .AttachedTemplate = "" ' Output an error report in the document from which the macro is run. ThisDocument.Range.InsertAfter vbCr & "Template: " & strPath & " not found for " & strDoc End If End If Else ' Output a 'protected' file report in the document from which the macro is run. ThisDocument.Range.InsertAfter vbCr & strDoc & " protected. Not updated." End If .Close SaveChanges:=True End With ' Update the main file counter j = j + 1 ' Let Word do its housekeeping DoEvents ' Reset the file's Date/Time stamp. Set oItem = FSO.GetFile(strDoc) If oItem.DateLastModified <> StrDtTm Then oItem.DateLastModified = StrDtTm Set oItem = Nothing End Sub Code:
Sub GetTemplateRef() With ActiveDocument MsgBox Dialogs(wdDialogToolsTemplates).Template End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
New Template window in Word 2007 | kerrk | Word | 1 | 10-14-2011 02:06 AM |
Missing standard office tabs in ribbon for word 2007 | pugs | Word | 5 | 09-23-2011 12:12 AM |
Need help creating a word 2007 resume template | gsw1 | Word | 0 | 09-27-2010 06:29 PM |
How to share macros in Powerpoint 2007?? | proshop | PowerPoint | 0 | 12-20-2009 07:29 AM |
Editing Default Word 2007 Template | nhrav | Word | 0 | 09-16-2008 05:08 AM |