Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Find and Replace in word

Posted on 2001-06-18
9
Medium Priority
?
395 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 2

Accepted Solution

by:
chandukb earned 200 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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:Roy Low
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

596 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