Re: Find and replace tabs

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Peter Hewett (nospam_at_xtra.co.nz)
Date: 05/31/04


Date: Tue, 01 Jun 2004 10:25:43 +1200

Hi Larry

Sometimes the Find object can behave strangely (not as you would expect). This has been
attributed to ghost properties from a previous S+R operation. So if you specify *every*
Property you know the object is correctly initialised. This is one of the standard bits
of code I pulled from my library:

Public Sub SearchAndReplaceInStory(ByVal rngStory As Word.Range, _
                                   ByVal strSearch As String, _
                                   ByVal strReplace As String)
    Do Until (rngStory Is Nothing)
        With rngStory.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = strSearch
            .Replacement.Text = strReplace
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchAllWordForms = False
            .MatchSoundsLike = False
            .MatchWildcards = False

            .Execute Replace:=wdReplaceAll
        End With
       Set rngStory = rngStory.NextStoryRange
    Loop
End Sub

As you can see it sets a few more properties.

HTH + Cheers - Peter
 

"Larry" <larry328@att.net>, said:

>Hi Peter,
>
>You know more about this than I do, but have you ever searched for a tab
>and not gotten a tab? Have you ever replaced found text by X and ended
>up replacing it by Y?
>
>So I'm interested: where does the possible ambiguity come in to a S&R?
>
>Larry
>
>
>
>
>"Peter Hewett" <nospam@xtra.co.nz> wrote in message
>news:r7dp901h77aqf3qiob5s8vni5vl8jk7ica@4ax.com...
>> Hi Larry
>>
>> If you want consistent reliable results when using th Find object I've
>found it prudent to
>> initialise it thoroughly. What's a few lines of code? I also think
>that it makes exactly
>> what you're searching for/replacing unambiguous.
>>
>> HTH + Cheers - Peter
>>
>>
>> "Larry" <larry328@att.net>, said:
>>
>> >Isn't a simple find and replace enough?
>> >
>> >Dim r As Range
>> >Set r = Selection.Range
>> >
>> > With r.Find
>> > .ClearFormatting
>> > .Replacement.ClearFormatting
>> > .Text = "^t"
>> > .Replacement.Text = ""
>> > .MatchWildcards = False
>> > .Forward = True
>> > .Wrap = wdFindContinue
>> > End With
>> > r.Find.Execute Replace:=wdReplaceAll
>> >
>>
>



Relevant Pages

  • Re: replace text string in word automatically
    ... Public Sub BatchReplaceAnywhere() ... Dim FirstLoop As Boolean ... 'Get the text to be replaced and the replacement ... For Each rngstory In ActiveDocument.StoryRanges ...
    (microsoft.public.word.vba.general)
  • Re: replace text string in word automatically
    ... Public Sub BatchReplaceAnywhere() ... Dim FirstLoop As Boolean ... 'Get the text to be replaced and the replacement ... For Each rngstory In ActiveDocument.StoryRanges ...
    (microsoft.public.word.vba.general)
  • Re: replace text string in word automatically
    ... Public Sub BatchReplaceAnywhere() ... Dim FirstLoop As Boolean ... 'Get the text to be replaced and the replacement ... For Each rngstory In ActiveDocument.StoryRanges ...
    (microsoft.public.word.vba.general)
  • Re: Changing Merge Field Names
    ... Public Sub BatchReplaceAnywhere() ... Dim FirstLoop As Boolean ... 'Get the text to be replaced and the replacement ... For Each rngstory In ActiveDocument.StoryRanges ...
    (microsoft.public.word.mailmerge.fields)
  • Re: Finding and replacing in Word using macros
    ... For Each rngStory In ActiveDocument.StoryRanges ... Public Sub SearchAndReplaceInStory(ByVal rngStory As Word.Range, ... "Dave Lett" wrote: ... >> But instead of replacing with a specific word everytime I need to replace ...
    (microsoft.public.word.vba.general)