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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Embedded Templates in Word Documents 1

Status
Not open for further replies.

ancientcontra

Technical User
Jun 30, 2005
42
GB
OK we have a few hundred files that have are based on a template that no longer exists on the network.

It is an embedded UNC, that can be found in Tools -> TEmplates and Add-Ins.

In the first line under document template - that is the old URL, that I would like to change in each document. I need some script that will trawl the directory and change this in every file to a new location.



Some notes :

Link Fixer DOES not work.

Global templates containing this file DO NOT work

Startup Directorys (Both) do not work.

Workgroup Templates cannot help.

The old url is in each and every document, no central way to change this as its code in the docs.
 
The usual way to deal with this is to set up a Redirect in Windows server pointing to the new location of the template. It saves a whole load of bother.


Regards: tf1
 
Sorry what do you mean by that? set a CNAME for the old server that points to the new?

the shares are different.

This is NOT workgroup templates / global templates, group policy cannot help, where do you suggest I do this ?

I am trying : but it is not working

Sub Test()
'
' Fix the links Macro
' Macro created 22/05/2007 by Talbot User
'
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 = "<\\AGY_OFFICE\DATA>"
NewServer = "<\\FS01\data$>"
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, 13)) = LCase(OldServer) Then
objDoc.AttachedTemplate = NewServer & Mid(strPath, 14)
End If
strFileName = Dir()
objDoc.Save
objDoc.Close
Loop
Set objDoc = Nothing
Set objTemplate = Nothing
Set dlgTemplate = Nothing

End Sub
 
If you know what the new template path and name is, you could try:
Code:
NewServer = new FULL Path and Name!
strFileName = Dir(strFilePath & "*.doc")
   Do While strFileName <> ""
      Documents.Open Filename:= strFilePath & strFilename
      With ActiveDocument
        .AttachedTemplate = NewServer
        .Save
        .Close
      End With
      strFileName = Dir
   Loop
There is no real need to make a document object since you are opening the file anyway. This just explicitly attaches a template to each file.

faq219-2884

Gerry
My paintings and sculpture
 
great thankyou I have that working now.

My problem is I would like to recurce subdirectories I have have just fount about 2000 directories, all over the place with these templates pinting to the old server !

Is it hard to recurse the directories?
 
I don't think you can recurse sub directories with Dir...um, maybe you can with a test for "..", but anyway you can change from using Dir, to using FileSystemObject.

A folder FileSystemObject has SubFolders as a property. You can do the top folder, then loop through all the subfolders. Of course performing action on each subfolder is pretty easy, but to check for sub-subfolders, you would have to not just USE the SubFolder property of the Folder, but make each one a new object and then check ITS SubFolder properties.

This could get to be a pain. But then you must know about that. Any file system with 2000 folders (directories) is a pain by definition.

I would strongly recommend that you make the action instructions for each file into a separate procedure, and call it. Keep your code clean.

Oh and you will need to make sure you have a reference to Microsoft Scripting Runtime in order to use FileSystemObject.

faq219-2884

Gerry
My paintings and sculpture
 
sorry, I am not sure even about the difference between a macro and a normal visual basic script. I think this is beyond my skills -

do I have to run this as a macro inside word ---

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, 18)) = LCase(OldServer) Then
objDoc.AttachedTemplate = NewServer & Mid(strPath, 19)
End If
strFileName = Dir()
objDoc.Save
objDoc.Close
Loop

That part there ought to be the prcedure - but I really have no idea how to use filesystemobject - whats the relationship between vbs scripts and ms word ?

CAn I use a normal VBS ?
Sorry I am very new to this, I havnt done programming for a few years.

Nick Cutting
MCSA CCNA A+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top