Automating find & replace

N

Neil Humphries

I have over 100 files to convert. I need to read a source file containing
multiple lines with the format "text to find TAB text to replace with". I
initially thought of using a .CSV file but encountered several problems:
1 The text strings themselves contain spaces and commas;
2 The text strings contain non-breaking spaces ;
3 The text strings contain non-breaking hyphens;
4 ASCII .txt files won't handle non-breaking spaces or hyphens.
5 Unicode .txt files won't handle non-breaking hypens either unless the
hyphen character is replaced with the coding from the insert symbol dialog
box.
6 Unicode .txt files don't show the non-breaking spaces any differently
than regular spaces making additions to or maintenance of the file almost
impossible.

In Word I can easily distinguish the different characters. How can I read a
Word .doc file and use each line to populate a find & replace operation?
I will need to open each file in a directory in turn and loop through all
the find & replace pairs for each file.

I can use TAB as a delimeter. If
 
D

Doug Robbins - Word MVP

Put the find and replace strings in the first and second columns
respectively of a table in a Word document, commencing with the second row
of the table.

The use code such as the following (in which you modify the PathToUse to
point to the folder containing the files in which you want the replacements
to be made and you modify Set Source = Documents.Open() to point to the file
that contains the abovementioned table.

Dim myFile As String
Dim PathToUse As String
Dim myDoc As Document
Dim Source As Document
Dim SourceTable As Table
Dim FindText As Range, ReplaceText As Range
Dim i As Long

PathToUse = "C:\Test\"
Set Source = Documents.Open("c:\Source.doc")
Set SourceTable = Source.Tables(1)
For i = 2 To SourceTable.Rows.Count
Set FindText = SourceTable.Cell(i, 1).Range
FindText.End = FindText.End - 1
Set ReplaceText = SourceTable.Cell(i, 2).Range
ReplaceText.End = ReplaceText.End - 1
myFile = Dir$(PathToUse & "*.doc")
While myFile <> ""
Set myDoc = Documents.Open(PathToUse & myFile)
myDoc.Activate
Selection.HomeKey wdStory
With Selection.Find
.ClearFormatting
.Text = FindText.Text
.Replacement.Text = ReplaceText.Text
.Execute Replace:=wdReplaceAll
End With
myDoc.Close SaveChanges:=wdSaveChanges
myFile = Dir$()
Wend
Next i


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
N

Neil Humphries

Thanks for the quick response. I'm putting fires out now, but should be able
to try your suggestion later this week.

I will try putting the table in the same document as the macro to simplify
the user's experience.
 
N

Neil Humphries

Thanks for pointing me in the right direction. Your code opened each file for
each search and replace pair. I modified the code to load the find and
replace pairs into an array so I could open each file once and loop through
all the find and replace pairs before closing the file.

Sub GetFindAndReplaceItems()
Dim SourceTable As Table
Dim FindText As Range, ReplaceText As Range
Dim I As Integer

I = 0
Set SourceTable = ActiveDocument.Tables(1)
For I = 2 To SourceTable.Rows.Count
ReDim Preserve arrfindreplace(1, I)
Set FindText = SourceTable.Cell(I, 1).Range
FindText.End = FindText.End - 1
arrfindreplace(0, I - 2) = FindText
Set ReplaceText = SourceTable.Cell(I, 2).Range
ReplaceText.End = ReplaceText.End - 1
arrfindreplace(1, I - 2) = ReplaceText
UserForm1.ListBox1.AddItem (FindText & " > " & ReplaceText)
Next I

End Sub

How can the line:
myFile = Dir$(PathToUse & "*.doc")
be modified to look for both *.doc and *.docx files?
"*.doc?" won't work for me as I don't want to include "*.docm" files.
 
B

brian

These instructions look like they will do what I want to do which is to
change a date in a bunch of documents - all in one folder. However, I have
no idea as to how to execute. Can you provide me with any assistance?
 
F

Fumei2 via OfficeKB.com

It use the Dir function. It is Help. But here is a simple example.

Sub Bunch_Of_Stuff()
Dim file
Dim path As String

path = "c:\yadda\"

file = Dir(path & "*.doc")
Do While file <> ""
Documents.Open Filename:=path & file
Call DoMyStuff
file = Dir()
Loop
End Sub

Sub DoMyStuff()
' your code to change the dates
' and assumably also save the ActiveDocument
End Sub

The procedure Bunch_Of_Stuff opens all .doc files in the folder and for each
one, executes the actioning procedure DoMYStuff.
These instructions look like they will do what I want to do which is to
change a date in a bunch of documents - all in one folder. However, I have
no idea as to how to execute. Can you provide me with any assistance?
Put the find and replace strings in the first and second columns
respectively of a table in a Word document, commencing with the second row
[quoted text clipped - 59 lines]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top