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!

Text Box help

Status
Not open for further replies.

pluz

Instructor
Jan 26, 2004
20
US
Hi everyone! I am using Word 2003. I created a form that users enter information into. Then, a macro runs that replaces text in a document with information entered into the form. For instance, it will search for *aa* and replace with whatever the user typed into the form for text box aa.

It works great, except my problem is: If the user does not add any text, the searched item is replaced with nothing. I don't want that. How can I make the macro skip the current search and move on to the next search if nothing is entered into a text box? Can you please help me with a solution? THANK YOU.

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "*aa*"
If tbAA.Text = "" Then
Resume Next
Else: .Replacement.Text = tbAA.Text
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "*A*"
.Replacement.Text = tba.Text
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "*bb*"
.Replacement.Text = tbBB.Text
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

 
need more details on design & purpose

is this formfield textbox?

is this ONE macro (if formfields, running as an onexit macro?), or multiple macros? looks like one macro running at end.

is this a UserForm with textboxes?

answer possible but details needed.

generally, better do validation first.

if txtAA.text <> "" then
---do replacement
end if

therefore if txtAA.text = "" nothing happens
 
UserForm, sorry.

when a user hits OK button, macro runs a search and replace.

 
ok, much easier.

do u want to MAKE them fill in textboxes?

if no, as posted, do validation on textbox values.

if txtAA.text <> "" then
-- do replacement
end if
if txtA.text <> "" then
-- do replacement
end if
if txtBB.text <> "" then
-- do replacement
end if

may help 2 know purpose. seem odd to do a replace of "aa" text, but sure there is reason. also, r u doing good error trapping? what if user enter not "", but "ndt6*23" - is this valid? Is ANYTHING other than blank valid?

May also want to do the replace as separate sub called, and use Range object, rather than Selection, also maybe build array of the searchfor text

Code:
Sub CommandButton1_Click()
Dim sReplaceText(2) As String
sReplaceText(0) = "*aa*"
sReplaceText(1) = "*A*"
sReplaceText(2) = "*BB*"

If TextBox1.Text <> "" Then
    Call myReplace(sReplaceText(0), TextBox1.Text)
End If
If TextBox2.Text <> "" Then
    Call myReplace(sReplaceText(1), TextBox2.Text)
End If
If TextBox3.Text <> "" Then
    Call myReplace(sReplaceText(2), TextBox3.Text)
End If
End Sub

Sub myReplace(strOriginal As String, strIn As String)
Dim r As Range
Set r = ActiveDocument.Content
r.Find.Execute FindText:=strOriginal, _
    ReplaceWith:=strIn, Replace:=wdReplaceAll
Set r = Nothing
End Sub
 
Brute force method, replace this:
If tbAA.Text = "" Then
Resume Next
Else: .Replacement.Text = tbAA.Text
By this:
.Replacement.Text = IIf(tbAA.Text = "", .Text, tbAA.Text)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top