Solved

Find and Replace in word

Posted on 2001-06-18
9
352 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

813 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now