Re: Automating find & replace
- From: Neil Humphries <NeilHumphries@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 1 Oct 2008 13:03:03 -0700
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.
"Doug Robbins - Word MVP" wrote:
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
"Neil Humphries" <NeilHumphries@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:F963CFE2-75F9-47D9-BBDB-602AA3439C5D@xxxxxxxxxxxxxxxx
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
- References:
- Re: Automating find & replace
- From: Doug Robbins - Word MVP
- Re: Automating find & replace
- Prev by Date: Re: Updating macros from Word 2003 to Word 2007
- Next by Date: Re: Word Index entry limitations?
- Previous by thread: Re: Automating find & replace
- Next by thread: Re: Automating find & replace
- Index(es):
Relevant Pages
|