p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

vba_word thread: Find a word and then replace it


Message #1 by "Dipak Goyal" <dipak.in@n...> on Thu, 18 Apr 2002 11:36:34 +0530



Might be a better way to do it, but here's some code thrown together that will
achieve the desired result:

    Dim counter As Integer
    counter = 1
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    Do While Selection.Find.Found = True
    With Selection.Find
        .Text = "ACT"
        .Replacement.Text = "ACT1"
        .Forward = True
        .Wrap = wdFindStop
    End With
    Selection.Find.Execute Replace:=wdReplaceOne
    Selection.moveright
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    Do While Selection.Find.Found = True
        counter = counter + 1
        With Selection.Find
            .Text = "ACT"
            .Replacement.Text = "ACT" & counter
            .Forward = True
            .Wrap = wdFindStop
        End With
        Selection.Find.Execute Replace:=wdReplaceOne
        Selection.moveright
    Loop




John Stendor <john.stendor@c...> on 04/18/2002 09:01:45 AM

Please respond to "VBA_Word" <vba_word@p...>

To:   "VBA_Word" <vba_word@p...>
cc:    (bcc: Jeff McHugh/PR Newswire)

Subject:  [vba_word] Re: Find a word and then replace it



Goto PLANET-SOURCE-CODE.com, you should be able to find some samples
there.

> Dipak Goyal wrote:
>
> Sir,
>
> Let me start with an example to explain u my problem. Suppose in a
> Word Document, I find a word "ACT" and now I want to replace the word
> "ACT" with "ACT1" in the first encounter and "ACT2", "ACT3",
> "ACT4" and so on till the end of the document. It should be case
> sensitive.
>
> How do I do it? I'm breaking my head over it, for sometime I couldn't
> get
> over it.
>
> Any help is appreciated.
>
> regards,
> Dipak Goyal
>








  Return to Index