Solved

Find and Replace in word

Posted on 2001-06-18
9
358 Views
Last Modified: 2010-05-02
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
Comment
Question by:philipsimmons
9 Comments
 
LVL 2

Accepted Solution

by:
chandukb earned 50 total points
ID: 6203336

    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
 
LVL 12

Expert Comment

by:Preece
ID: 6204458
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
 
LVL 2

Expert Comment

by:damienm
ID: 6206090
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: 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.

 
LVL 2

Expert Comment

by:damienm
ID: 6206098
That should have another quote around "b"

i.e.

Dim strVal As String
strVal = "abcaa"

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

= bbcbb
0
 

Author Comment

by:philipsimmons
ID: 6206400
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
 
LVL 2

Expert Comment

by:chandukb
ID: 6206524
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
 
LVL 2

Expert Comment

by:damienm
ID: 6206584
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
 

Expert Comment

by:roylow
ID: 6209071
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
 

Expert Comment

by:costello
ID: 6400346
Rejecting proposed answer.

Force accepting chandukb's comment.

costello
Community Support Moderator @ Experts-Exchange
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question