View Single Post
  #6   Report Post  
Posted to microsoft.public.word.docmanagement
Jay Freedman Jay Freedman is offline
external usenet poster
 
Posts: 9,854
Default I would like to find and replace a list of words with numbers

On Wed, 1 Oct 2008 17:46:03 -0700, checkQ
wrote:

The names are in text form.

What the code should do is look for Marcus Bowes and replace it with the
number 12. Or instead of replacing Marcus Bowes with 12 it could insert the
number 12 at the top of the page. This would be two separate macros in case
the other is not doable.

The body of the page would have Marcus Bowes and at the top of the page it
would have 12. On the next page where we have Norman Hilton, the macro would
search for the name Norman Hilton and insert the number 13. On the page where
there is Nancy Bates the macro would insert the number 2.

Or instead of inserting the number 2 at the top of the page, the macro would
search for Nancy Bates and replace Nancy Bates with the number 2. Of course I
could us the "find and Replace" option but I can only do that one at a time
for 99 pages.

The pages are separated by "page break"


Hmm... we're getting closer. Does "The names are in text form" mean they're in a
file with a .txt extension? If so, what is the path and file name of that file?
What separates the names from the code numbers -- are there tab characters as in
the sample you posted earlier, in some cases with spaces also, or something
else?

And are you sure it's going to be OK to completely replace the name on the
document page, so the name doesn't appear any more and only the code number
appears?

The reason I asked about the page break was to find out whether the document is
separated into "sections" (in Word lingo, meaning areas between section breaks),
which is typical output of a mail merge. It is not, which means that the code
numbers cannot be put into the header area because it would show the same value
on every page.

On the chance that I'm close to understanding what you want, the following macro
might do it. You'll have to replace "code.txt" in the macro with the actual name
of the text file containing the names and codes; and either put that file in
your Documents folder or change the Options.DefaultFilePath(wdDocumentsPath) to
the actual path where the file exists.

Sub NamesToCodes()
Dim WorkFile As Document
Dim CodeFile As String
Dim CodeLine As String
Dim LastSpacePos As Long
Dim CodeArray() As String
Dim ArrayLen As Long
Dim idx As Long
Dim SearchRg As Range

Set WorkFile = ActiveDocument

' Load the array with data from codefile
' (in this example, a file "codes.txt" in the
' Documents folder
CodeFile = Options.DefaultFilePath(wdDocumentsPath) _
& "\codes.txt"
ArrayLen = 0
Open CodeFile For Input As #1
While Not EOF(1)
Line Input #1, CodeLine

' assume that there is at least one tab
' between the name and the code number, and
' no tabs after the number
LastSpacePos = InStrRev(CodeLine, vbTab)

If LastSpacePos 0 Then
ReDim Preserve CodeArray(1, ArrayLen)
CodeArray(0, ArrayLen) = Replace( _
Trim(Left(CodeLine, LastSpacePos - 1)), _
vbTab, "")
CodeArray(1, ArrayLen) = _
Trim(Right(CodeLine, Len(CodeLine) - LastSpacePos))
ArrayLen = ArrayLen + 1
End If
Wend
Close #1

' Replace the first occurrence of each name (if found)
' with the corresponding code
For idx = 0 To ArrayLen - 1
Set SearchRg = WorkFile.Range
With SearchRg.Find
.Text = CodeArray(0, idx)
.Replacement.Text = CodeArray(1, idx)
.Forward = True
.Format = False
.Wrap = wdFindStop
.Execute Replace:=wdReplaceOne
End With
Next idx
End Sub

See http://www.gmayor.com/installing_macro.htm if you need instructions.


--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.