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

Loop me up!

Status
Not open for further replies.

Dreamboat

Instructor
Nov 7, 1999
4,881
US
I ask so few questions here, I refuse to feel guilty asking for free coding. :) The person this is for answers MANY questions at many free forums too, so I felt we could give them some help. They need to rename their network path, so it negates a bunch of hyperlinks in documents. I'd LOVE to do this for the guy:

An Excel workbook or maybe a Word table wherein he can type the FROM (old path) and the TO (new path). It opens each Word document in *this* folder and any subfolders of *this* folder (we'll have him move the file around to various folders), and turns field codes on, runs the find and replace (which won't work with field codes off), turn the field codes back on, save and close the file. I did record a macro that'll do it in one file, but I didn't save and close. Here's the code, cleaned up at least a little bit:

Sub Macro2()

ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "\\\\newtonst-dc\\Gail\\"
.Replacement.Text = "\\\\unit2-dc\\Gail\\"
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll
ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
'Save and close the file
'Loop through all files in this folder and subfolders
End Sub


I sooo much appreciate any help!!


Anne Troy
 
Hi Anne,

FileSearch will give you all the Files in a Folder (and its SubFolders if you ask it to), so that takes care of the Document loop.

As you mention a Table, I assume there may be more than one path to change so this code works with the first Table in the 'Control' Document processing each row and, where the first column is non-empty, picking up the contents of the first column (as old path) and the second column (as new path) ..

Code:
[blue]Dim ControlDoc As Document
Dim ControlTable As Table
Dim ControlRow As Row

Dim OldPath As String
Dim NewPath As String

Set ControlDoc = ActiveDocument
Set ControlTable = ControlDoc.Tables(1)

With Application.FileSearch

    .NewSearch
    .FileName = "*.doc"
    .LookIn = ControlDoc.Path
    .SearchSubFolders = True
    .Execute

    For i = 1 To .FoundFiles.Count
        If .FoundFiles(i) <> ControlDoc.FullName Then
            Documents.Open (.FoundFiles(i))
            For Each ControlRow In ControlTable.Rows
                If Len(ControlRow.Cells(1).Range.Text) > 2 Then
                    OldPath = Left(ControlRow.Cells(1).Range.Text, Len(ControlRow.Cells(1).Range.Text) - 2)
                    NewPath = Left(ControlRow.Cells(2).Range.Text, Len(ControlRow.Cells(2).Range.Text) - 2)
                
[green]                    ' Put your Find and Replace code here
                    ' .. OldPath is the Find Text, and
                    ' .. NewPath is the Replacement Text
[/green]                
                End If
            Next
            ActiveDocument.Close
        End If
    Next
    
End With[/blue]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top