Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 403
  • Last Modified:

Find and Replace in word

Ok...seems the crux of my problem with this program deals with my find and replace routines...

Can someone give me an example of a find and replace routine (where I search word for text, and replace that text)

Thanks

Philip
0
philipsimmons
Asked:
philipsimmons
1 Solution
 
chandukbCommented:

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "F"
        .Replacement.Text = "99"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

This is generated through a Macro and it works.

Hope this helps.

Chandu
0
 
PreeceCommented:
give this a try:

Public Function gfFindAndReplace(sString As String, sSearchString As String, sReplacementString) As String

    Dim lX As Long
   
    lX = InStr(1, sString, sSearchString)
   
    If lX > 0 Then
        gfFindAndReplace = Mid(sString, 1, lX - 1) & sReplacementString & _
        Mid(sString, lX + Len(sSearchString), Len(sString) - ((lX - 1) + Len(sSearchString)))
    Else
        gfFindAndReplace = sString
    End If
End Function
0
 
damienmCommented:
Why don't you just use the replace function in VB

Dim strVal as string
strVal = "abcaa"

msgbox replace(strVal,"a",b")

this will replace all the a's with b's

i.e.  bbcbb
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
damienmCommented:
That should have another quote around "b"

i.e.

Dim strVal As String
strVal = "abcaa"

MsgBox Replace(strVal, "a", "b")

= bbcbb
0
 
philipsimmonsAuthor Commented:
Yea, but I want to search an entire word document, several times for different terms...Do I have to define the entire word docuemnt as a string to make this work?
0
 
chandukbCommented:
Load your Old Values, and New Values in to Arrays.
OldArray(10) 'Number of times u want to loop
NewArray(10)

Then,

For I = 1 to UBOUND(OldArray)
   Selection.Find.ClearFormatting
   Selection.Find.Replacement.ClearFormatting
   With Selection.Find
       .Text = OldArray(I) 'This is the Old Value
       .Replacement.Text = NewArray(I) 'New Value
       .Forward = True
       .Wrap = wdFindContinue
       .Format = False
       .MatchCase = False
       .MatchWholeWord = False
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
   End With
   Selection.Find.Execute Replace:=wdReplaceAll
Next

Chandu



0
 
damienmCommented:
For my solution you have to define the whole word document as a string and then use a replace for each item.

Dim strVal As String
strVal = "abcaa"
strval = Replace(strVal, "a", "b")
strval = Replace(strVal, "c", "d")

This will return

bbdbb

But if you don't want to do this I am sure there is something inside word reference which lets you replace text.

Add 'Microsoft Word 9.0' to your object references.

Preece I don't think you should propose an answer to a question, in expert's exchange people generally leave comments and leave the person who asked the question the choice of which answer they want to accept.  This leaves the question open longer and gives more people a chance to give views.
0
 
Roy LowCommented:
Have a look at an app I have posted on my personal web page
www.low.net.au

It is a multi-file search and replace for both text and .doc files.

follow the link on the first page and download the source and/or the install program with example

Interested if you find it useful.
roy@low.net.au
0
 
costelloCommented:
Rejecting proposed answer.

Force accepting chandukb's comment.

costello
Community Support Moderator @ Experts-Exchange
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now