Re: Automating find & replace



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



.



Relevant Pages

  • Re: Automating find & replace
    ... Put the find and replace strings in the first and second columns ... to be made and you modify Set Source = Documents.Opento point to the file ... Dim PathToUse As String ... ASCII .txt files won't handle non-breaking spaces or hyphens. ...
    (microsoft.public.word.vba.general)
  • RE: UDF behaving very oddly -assistance or please suggest alternat
    ... OK i have used IF formulas to test whether the text strings in the cells are ... "Tom Ogilvy" wrote: ... Dim cs As Integer ...
    (microsoft.public.excel.programming)
  • Re: characters...
    ... Below is a subroutine that assumes your original list is in A1:A10. ... modify for the actual location of the original strings as well as for the ... Dim rngCell As Range ... you also say that you only want the first letter of the last ...
    (microsoft.public.excel.programming)
  • Problem beim Vergleich von Strings
    ... Ich habe ein suspektes Problem beim Vergleichen von Strings unter Excel ... Dim datenArray() As DatenEintrag ...
    (microsoft.public.de.excel)
  • Re: Remove from a collection
    ... Dim sTmr As String ... Dim lCount As Long ... For 10000 items of Longs ... For 1000 items of Strings ...
    (microsoft.public.vb.general.discussion)