Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using vba to modify word documents 1

Status
Not open for further replies.

bookouri

IS-IT--Management
Feb 23, 2000
1,464
US
I have thousands of word documents that have references to their original templates that were on one server but have been moved to a new server. This reference to a non-existent location makes the files take several minutes to load. I need to go through all the .doc files and replace the references to the old server with the new one. Ive found lots of examples of how to do something like this but cant get any of them to actually work. Ive got this far with a script I found that will actually run. It will open all the word docs. But when i check the .doc files that it has processed, the old server reference has not been changed. Can anybody point me toward what might be going wrong or point me in another direction if this wont work?


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 = "\\oldserver\share1\wordtemps"
NewServer = "\\newserver\share1\wordtemps"
strFilePath = InputBox("What is the folder location that contains the documents?")
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
 
I think you can run the Find and Replace tool from within VBA. That may very well be your best option.

Also, look to see if the reference is set in code somewhere, rather than a static location. If so, then you're going to need code to loop through the modules of each document, changing the references there.
 



Hi,

Code:
Debug.Print NewServer & Mid(strPath, 22)
See what you actually are trying to assign to the attachedtemplate property.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
thanks, i did finally notice that the original script was designed for a specific length of path.. that reference to strpath 21 and 22 matched the original example path but not mine..

 



Most of the time, the answer is at hand. You just have to find it.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
If LCase(Left(strPath, Len(OldServer))) = LCase(OldServer) Then
objDoc.AttachedTemplate = NewServer & Mid(strPath, 1 + Len(OldServer))
End If

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
thanks phv, that's much better.. i was really surprised to find those numbers hard coded in.. thats my excuse for not seeing it sooner anyway.. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top