Multiple search-replace in one run ? Word XP

M

MoiMeme

Hi,

I am trying to build a vba routine to :
- allow the user to input 4 strings
- then search for the four following strings in a template
: -V1, -V2, -V3, -V4 and replace them with the four strings the user has
given

I have made one that functions, but very crude and repetitive :
- Inputbox four times for the four input strings : is there a way get them
in one operation ? For example separated by a ";" ? How ? Other way ?
It would be nice to have to hit only once thje <OK> button !

- run four times the find & replace . Is there a way to have word make one
pass and replace the four -Vx patterns with the strings the user gave ?

TIA !!!
 
J

Jay Freedman

To collect four strings from the user in one pass, create a userform
(http://www.word.mvps.org/FAQs/Userforms/CreateAUserForm.htm) with four text
entry boxes and an OK button (and probably a Cancel button). Your macro can
display the userform and then retrieve the user's entries when the user
clicks the OK button to dismiss it.

There is no alternative to running four separate Find operations. However,
you don't have to _write_ four separate sections of code. Store the four
search strings and the four replacements in arrays, and write one loop that
executes four times, once for each index into the array. Here's some sample
code:

Sub demo()
Dim oRg As Range
Dim i As Long
Dim Search(4) As String
Dim Repl(4) As String

Search(0) = "-V1"
Search(1) = "-V2"
Search(2) = "-V3"
Search(3) = "-V4"

Repl(0) = "X1" ' get these from the userform
Repl(1) = "X2"
Repl(2) = "X3"
Repl(3) = "X4"

For i = 0 To 3
Set oRg = ActiveDocument.Range
With oRg.Find
.Text = Search(i)
.Replacement.Text = Repl(i)
.Format = False
.Forward = True
.Wrap = wdFindStop
.Execute Replace:=wdReplaceAll
End With
Next i
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
F

fumei via OfficeKB.com

Use a userform.

If (and I may not understand this correctly), the V1, V2, V3, V4 strings
(that you are searching FOR) are constant, then make a userform with four
textboxes labeled String1, String2, String3, String 4 - or whatever you like.

Have a commandbutton (named OK). On clicking OK, the code on the userform
can do the find/replace.

You do not specify the exact logic you need, so I will make assumptions.

"V1" - all found instance to be replaced by the text put in textbox1 (say
named String1)

"V2" - all found instance to be replaced by the text put in textbox1 (say
named String1)

etc.

If this is so...here is some code for the OK button. Note I will use the
names of the textboxes as "String1"...etc.

Sub OK_Click()
Dim myReplace()
Dim mySearch()
Dim r As Range
Dim j As Long

mySearch = Array("V1", V2", "V3", "V4")
' now have array of strings to search FOR

myReplace = Array(String1.Text, _
String2.Text, _
String3.Text, _
String4)

' now have an array of the four strings the user put in the four textboxes
' to be used to replace strings in mySearch

Set r = ActiveDocument.Range
For j = 0 to Ubound(mySearch)
With r.Find
Do While .Execute(Findtext:=mySearch(j), Forward:=True) _
= True
' replace mySearch text with myReplace text
r.Text = myReplace(j)
' collapse to the end of current range
r.Collapse 0
' and continue
Loop
End With
' reset r to whole document for the next item
' in mySearch
Set r = ActiveDocument.Range
Next j

' unload userform
Unload Me


The above goes through each item in the mySearch array, and replaces each
item with th ematching item in the myReplace array - i.e. V1 replaced with
String1.Text. With String1 being a textbox on the userform.

NOTE!!!!!!! This does NOT cover any possible error-trapping, such as...what
if the user does not put anything into a textbox? This would be easy to do.
What if they put in gibberish? Do you need to test for the strings being
replaced?
 
F

fumei via OfficeKB.com

Darn, I should have refreshed before posting. Essentially mine is the same
as what Jay posted.
Use a userform.

If (and I may not understand this correctly), the V1, V2, V3, V4 strings
(that you are searching FOR) are constant, then make a userform with four
textboxes labeled String1, String2, String3, String 4 - or whatever you like.

Have a commandbutton (named OK). On clicking OK, the code on the userform
can do the find/replace.

You do not specify the exact logic you need, so I will make assumptions.

"V1" - all found instance to be replaced by the text put in textbox1 (say
named String1)

"V2" - all found instance to be replaced by the text put in textbox1 (say
named String1)

etc.

If this is so...here is some code for the OK button. Note I will use the
names of the textboxes as "String1"...etc.

Sub OK_Click()
Dim myReplace()
Dim mySearch()
Dim r As Range
Dim j As Long

mySearch = Array("V1", V2", "V3", "V4")
' now have array of strings to search FOR

myReplace = Array(String1.Text, _
String2.Text, _
String3.Text, _
String4)

' now have an array of the four strings the user put in the four textboxes
' to be used to replace strings in mySearch

Set r = ActiveDocument.Range
For j = 0 to Ubound(mySearch)
With r.Find
Do While .Execute(Findtext:=mySearch(j), Forward:=True) _
= True
' replace mySearch text with myReplace text
r.Text = myReplace(j)
' collapse to the end of current range
r.Collapse 0
' and continue
Loop
End With
' reset r to whole document for the next item
' in mySearch
Set r = ActiveDocument.Range
Next j

' unload userform
Unload Me

The above goes through each item in the mySearch array, and replaces each
item with th ematching item in the myReplace array - i.e. V1 replaced with
String1.Text. With String1 being a textbox on the userform.

NOTE!!!!!!! This does NOT cover any possible error-trapping, such as...what
if the user does not put anything into a textbox? This would be easy to do.
What if they put in gibberish? Do you need to test for the strings being
replaced?
[quoted text clipped - 13 lines]
 
F

fumei via OfficeKB.com

Except Jay's uses ReplaceAll (better in most cases), and mine does not hard-
code things as 4 items. The arrays could of any number of items.
Darn, I should have refreshed before posting. Essentially mine is the same
as what Jay posted.
Use a userform.
[quoted text clipped - 68 lines]
 
M

MoiMeme

I have never used userforms. Have found how to create one, display textboxes
and Ok/cancel buttons.
But haven't yet found how to retrieve the values of teh textboxes, and how
to check for empty fields.
Where can I find details ?
I have a module with the code to replace the text with new values, and call
the userform from there.
TIA
 
J

Jay Freedman

Let's say the userform is named frmGetReplacements (you can change the name from
the default UserForm1 in the Properties pane of the VBA editor), it contains
text boxes named TextBox1 through TextBox4 (the default names, although you
should change these, too) and a command button, and the code in the command
button's click procedure just says

Me.Hide

Then the code in the module to show the userform and get the values out of the
text boxes would look something like this:

Sub demo2()
Dim Repl(4) As String
Dim ufrm As frmGetReplacements
Set ufrm = New frmGetReplacements

With ufrm
.Show
Repl(0) = .TextBox1.Value
Repl(1) = .TextBox2.Value
Repl(2) = .TextBox3.Value
Repl(3) = .TextBox4.Value
End With

Set ufrm = Nothing

' rest of code...
End Sub

The first Set statement creates an 'instance' of the userform -- making a copy
for use, in the same way that using File > New makes a document from a template.
The copy is named ufrm (this is just a variable name, which you can make up like
any variable name).

The .Show statement puts the userform on the screen and temporarily gives it
control of the keyboard and mouse. When the user clicks the command button, the
click procedure causes the userform to be hidden, and that gives control back to
the macro. The four assignments take the user's entries from the text boxes and
put them into the Repl array. Note that the dot in front of each TextBox name
means "member of the object in the With statement".

Finally, the second Set statement tells VBA to discard the instance ufrm,
removing it from memory. After that point, the TextBoxes no longer exist, but
their values have been stored in the Repl array which you can use in the
remaining code.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
M

MoiMeme

Thansk a lot

Got it to work now .
Regards

Jay Freedman said:
Let's say the userform is named frmGetReplacements (you can change the
name from
the default UserForm1 in the Properties pane of the VBA editor), it
contains
text boxes named TextBox1 through TextBox4 (the default names, although
you
should change these, too) and a command button, and the code in the
command
button's click procedure just says

Me.Hide

Then the code in the module to show the userform and get the values out of
the
text boxes would look something like this:

Sub demo2()
Dim Repl(4) As String
Dim ufrm As frmGetReplacements
Set ufrm = New frmGetReplacements

With ufrm
.Show
Repl(0) = .TextBox1.Value
Repl(1) = .TextBox2.Value
Repl(2) = .TextBox3.Value
Repl(3) = .TextBox4.Value
End With

Set ufrm = Nothing

' rest of code...
End Sub

The first Set statement creates an 'instance' of the userform -- making a
copy
for use, in the same way that using File > New makes a document from a
template.
The copy is named ufrm (this is just a variable name, which you can make
up like
any variable name).

The .Show statement puts the userform on the screen and temporarily gives
it
control of the keyboard and mouse. When the user clicks the command
button, the
click procedure causes the userform to be hidden, and that gives control
back to
the macro. The four assignments take the user's entries from the text
boxes and
put them into the Repl array. Note that the dot in front of each TextBox
name
means "member of the object in the With statement".

Finally, the second Set statement tells VBA to discard the instance ufrm,
removing it from memory. After that point, the TextBoxes no longer exist,
but
their values have been stored in the Repl array which you can use in the
remaining code.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup
so all
may benefit.
 

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