Solved

Find and Replace in word

Posted on 2001-06-18
9
343 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…

758 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

21 Experts available now in Live!

Get 1:1 Help Now