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

Find & Replace in MS Word 1

Status
Not open for further replies.

coxdon

Technical User
Mar 15, 2005
10
US
I'm trying to use the Find & Replace feature in Word within a Do Loop statement to sequentially replace the same text string with different variables. My question is how to structure the Do statement to stop when it no longer finds the text string to be replaced. The code I'm using is:

Code:
Do Until ???????

Selection.HomeKey Unit:=wdStory
    With Selection
       	If .Find.Forward = True Then
            .Collapse Direction:=wdCollapseStart
        Else
            .Collapse Direction:=wdCollapseEnd
        End If
.Find.Execute “variable”, True, False, False, False, False, True, , , “replacement variable”, wdReplaceOne
    End With
Loop

I can't just use wdReplaceAll, because each instance of the text I'm looking for will be replaced with a different variable (for instance, it should replace the letter "a" with a different variable each time it finds it.)

I've figured out how to count the number of occurrences of the text I'm looking for, then execute the Do statement that number of times, but I'm thinking there's got to be a more efficient way to do it.

Thanks!

 
Hey Coxdon,
Try this, no loop, just this code in your subroutine.

Code:
With oRange.Find
    .Text = "*lettter*"
    .Replacement.Text = "*Variable*"
    .Execute Format:=False, Replace:=wdReplaceOne
End With



Jay
 
Thanks, that code's much more efficient, but how do you suggest I recurse and drop out if it finds nothing?
Don
 
Hey Don,
Well I suggest you make an array for your variables, and have a counter variable that you would use as an index. And before every time you resursivly call the function, you increment the index. Then you need a function that passes in the array and the index. Now for how to stop, I'm not sure... :S. I have done similar things in C, not VBA. Maybe someone else can help here.

Hope this helps :-D.



Jay
 
Hey Don,
did some research for you, cause it bothered me that I didn't know how do that part :-D. Anyways, I found how to do it. Now it would be way to nice to give the code. Not to mention that you won't learn. Anyways I'll hint you in the right direction for sure. Type find in the help , in the VBA editor, and look under find property. Now just look at the code there and modify it to your needs.

If you need more help, I'll be glad to help.

Have fun.



Jay
 
Jay,
Been there and done that, but I don't understand enough about how the find/replace function works to get the code to work. Using your simplified replace routine and the sample Do statment in the help file, here's the best I can do:
Code:
Sub test()
Dim count
Dim num

count = 0

[COLOR=red]'Count the iterations of the letter "a" first[/color]

With ActiveDocument.Content.Find
  Do While .Execute(FindText:="a", Forward:=True, MatchCase:=True) = True
    count = count + 1
  Loop
End With

[COLOR=red]'Now go back and replace them one at a time[/color]
If count > 0 Then
  For num = 1 To count
    With ActiveDocument.Content.Find
      .Text = "a"
      .Replacement.Text = "Z"
      .Execute Format:=False, Replace:=wdReplaceOne, MatchCase:=True
    End With
   Next num
End If

End Sub

This will run the "Replace:=wdReplaceOne" statment repeatedly so that by replacing the text "Z" with a variable that I can cycle through, I can replace successive iterations of the letter "a" with different values. The challenge is to combine the With statements, so that I don't have to count first, but can simply replace them the first time through. But it's defeated me! Perhaps someone very familiar with Word functions can figure it out. Anyhoo, thanks for your help! It works, so I'm not complaining!!
Don
 
You should declare your variables as explicit data types.

Dim count will make a variant. You really only need a integer. Actually, if you use a Range object you do even need that. Here you go.
Code:
Sub test()
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
  Do While .Execute(Findtext:="a", Forward:=True) = True
    Do
     .Replacement.Text = "Z"
     .Execute Format:=False, Replace:=wdReplaceAll, _
        MatchCase:=True
    Loop Until .Found = False
  Loop
End With
Set r = Nothing
End Sub

Note of course that both Findtext and Replacement.Text can be string variables. Therefore you can search and replace anything you want.

I must point out that this is essentially Find and Replace, and there is no reason for it, as Word already HAS a Find and Replace.

The original post was a doing a Replace with different things, variables. I do not see how you can do this without having your Replacement.Text come from an array. This adds a complication.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top