Search and Replace Text Across Multiple Lines

R

Ryan Jamison

I'm trying to search and replace text across multiple lines in a Word file.
For example, I have three lines with the text:

Line 1
Line 2
Line 3

And I want to be able to find all of those at one time, i.e. highlight all
of them. Then I would like to replace the three lines with one line of text,
"Line 4", giving me:

Line 4

Is this possible and if so, how so? I'm a novice user of Visual Basic, so I
am open to Visual Basic solutions as well. Thanks

Ryan
 
J

Jay Freedman

Ryan said:
I'm trying to search and replace text across multiple lines in a Word
file. For example, I have three lines with the text:

Line 1
Line 2
Line 3

And I want to be able to find all of those at one time, i.e.
highlight all of them. Then I would like to replace the three lines
with one line of text, "Line 4", giving me:

Line 4

Is this possible and if so, how so? I'm a novice user of Visual
Basic, so I am open to Visual Basic solutions as well. Thanks

Ryan

I don't think it's possible without a macro, but easy enough with one:

Sub demo()
Dim sFindWhat As String
Dim sReplaceWith As String
Dim oRg As Range

Set oRg = ActiveDocument.Range

sFindWhat = Selection.Text
sReplaceWith = InputBox("Enter the replacement:") & vbCr

With oRg.Find
.Text = sFindWhat
.Replacement.Text = sReplaceWith
.Forward = True
.Wrap = wdFindStop
.Execute Replace:=wdReplaceAll
End With
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
R

Ryan Jamison

Got it! Thanks Jay, I appreciate the help!

Jay Freedman said:
I don't think it's possible without a macro, but easy enough with one:

Sub demo()
Dim sFindWhat As String
Dim sReplaceWith As String
Dim oRg As Range

Set oRg = ActiveDocument.Range

sFindWhat = Selection.Text
sReplaceWith = InputBox("Enter the replacement:") & vbCr

With oRg.Find
.Text = sFindWhat
.Replacement.Text = sReplaceWith
.Forward = True
.Wrap = wdFindStop
.Execute Replace:=wdReplaceAll
End With
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top