Word Macro - Copy/Paste/Highlight

montanaheather

New Member
Joined
Oct 31, 2011
Messages
25
Hello, again,

Thank you for the help before. It's fun to learn more about Word macros!

The macro to find search terms is working. I am trying to learn how to find and highlight the search terms in the new document (where the search results are pasted from the original document). I have the code below, but it's not highlighting anything in either document. I feel like this is a super simple fix, but I cannot figure it out. Any help is MUCH appreciated!!!

Thanks!

CODE (the green bolded text is where I think the problem is...but I am not sure...)

Sub GASBWordSearch()
Dim SearchTerm As String
Dim WordsAfter As Long
Dim WordsBefore As Long
Dim i As Long
Dim Rng As Range
Dim Doc As Document
Dim RngOut As Range

On Error GoTo ErrHandler:

SearchTerm = InputBox("Enter your search terms, Maria Cancro!" & vbCr & _
"Then, sit back, relax, and let this macro do some heavy lifting." & vbCr & _
vbCr & "It's okay - it works out!")
SearchTerm = LCase(Trim(SearchTerm))
If Len(SearchTerm) = 0 Then Exit Sub
WordsBefore = InputBox("Enter the number of words before your search term to find.")
WordsAfter = InputBox("Enter the number of words after your search term to find.")
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = SearchTerm
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.Execute
End With
If .Find.Found Then
Set Doc = Documents.Add(Visible:=True)
Do While .Find.Found
Set Rng = .Duplicate
With Rng
.MoveStart wdWord, -WordsBefore
.MoveEnd wdWord, WordsAfter + 1
.Select
If MsgBox(.Text, vbYesNo, "Copy this block?") = vbYes Then
.Copy
With Doc
.Range.InsertAfter vbCr
Set RngOut = .Characters.Last
RngOut.Paste
' RngOut.HighlightColorIndex = wdTurquoise
End With
End If
End With
.Collapse wdCollapseEnd
.Find.Execute
Loop
Doc.Activate
ActiveWindow.Visible = True
End If
End With

' HighlightSearchTerm Macro
' Highlights your search terms IN THE NEW DOCUMENT ONLY so you can easily see what you searched for later
Doc.Activate
Selection.Find.ClearFormatting
With Selection.Find
.Text = SearchTerm
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Doc.Activate

Exit Sub
'exits sub if there was a data entry error above
ErrHandler:
' error handling code
If MsgBox("Oops! It seems there was an error in your request. Please run your search again. Hit Ctrl+M+C and give it another go!", vbOKOnly, "Search Error") = vbOK Then
Exit Sub
End If
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

Forum statistics

Threads
1,225,609
Messages
6,185,984
Members
453,333
Latest member
BioCoder84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top