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

Run "Selection.Find.Replacement.ClearFormatting" - VBA

Status
Not open for further replies.

BJ1106

Programmer
Aug 24, 2006
30
US
Hi,

I am using a macro to search one word and replace it using a different one. For example:

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Exceeds Expectations" '20
.Replacement.Text = "Excellent "

How can i not replace the ones that were showing in a textbox formfield where user entered.

Thanks for any help.
 
How are you unprotecting? Try this.
Code:
Sub ReplaceMe()
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
    ActiveDocument.Unprotect
End If
With Selection
    .HomeKey unit:=wdStory
    With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "Exceeds Expectations"
        .Replacement.Text = "Excellent           "
        .Forward = True
        .Execute Replace:=wdReplaceAll
    End With
End With
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset:=True
End Sub
Of course, if you have a password for Forms, you would have to add that.

Gerry
My paintings and sculpture
 
Gerry,

I tired. But if there are "Exceeds Expectations" in text formfields which is user entered and suppose not be replaced was replaced with "Excellent" also.

 
Let me make my question clearly:

I am trying to write a macro to replace a word from a template but not in text formields.

For example, I have a checkbox, and put a description next to this check box as "Exceeds Expectations". now I want to run the macro to replace "Exceeds Expectations" with the word "Excellent". but meanwhile, user could possible type in many times of "Exceeds Expectations" word in different text formfileds. when i run the macro, i don't want to replace these "Exceeds Expectations".

Thanks.
 
What about doing the replace BEFORE the user types anything ?
 
Well, when to call the macro to replace the words is ondemand by user not me which means they would like to run this macro anytime they want to change the words.
 
Well, I found out a new thing about formfields. They are very odd things when using the Selection object. They can be selected, but unless you resize the Selection to include at least ONE character outside the formfield, the formfield count is NEVER 1. Strange. So, here you go:
Code:
Sub ReplaceMe2()
If ActiveDocument.ProtectionType = _
         wdAllowOnlyFormFields Then
    ActiveDocument.Unprotect
End If
With Selection
 .HomeKey Unit:=wdStory
 With .Find
   .ClearFormatting
   .Replacement.ClearFormatting
   Do While (.Execute(FindText:="Exceeds Expectations", _
         Forward:=True) = True) = True
[COLOR=red]' increase the Selection by one character[/color red]
      Selection.MoveStart Unit:=wdCharacter, Count:=-1
      [COLOR=red]' check if Selection has a formfield count
      ' if it was in a formfield, the count should = 1[/color red]
      If Selection.FormFields.Count <> 1 Then
        [COLOR=red]' it was NOT in a formfield, so
        ' resize Selection back to original, delete it,
        ' and insert text[/color red] 
        With Selection
            .MoveStart Unit:=wdCharacter, Count:=1
            .Delete
            .TypeText Text:="Excellent           "
         End With
       Else
         [COLOR=red]' otherwise, just collapse selection
         ' and continue on with loop[/color red]
         Selection.Collapse Direction:=wdCollapseEnd
       End If
    Loop
 End With
End With
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset:=True
End Sub
This will replace the searched for text, but only if it is NOT in a formfield.

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top