I have about 6 months of limited vba Word macro experience. I have been searching and experimenting for weeks to search a selection for intact words and multi-word phrases found from an array and list all found in an InsertAfter the selection, regardless of single words or multiple word phrases. The macro below does the InsertAfter the selection with the single words, but the multiple word phrases are omitted. I have another macro that highlights both the single words and mulitiple word phrases, but am unable to figure out how to InsertAfter the selection words and phrases found in that one as well. I would like a macro to list both the single words and multiple word phrases found from an Array (without having to go to an outside .doc) and insert a comma separated InsertAfter the selection. See below for an example and macro. Is it possible to tweak the macro below to search, find, and InsertAfter a selection both single words and multiple word phrases? Any help is appreciated. Thanks.
-macrointraining
For example, for the selection:
*Designing this project through the ups and downs*
searching for the following with a selection of the above using:
myWords = Array("through", "this project", "ups and downs")
yields an InsertAfter the selection of:
(myWords found: through)
The "this project" and "ups and downs" are omitted. I would like all words and phrases as found in InsertAfter, for example (myWords found: through, this project, ups and downs). Phrases are to be intact.
Sub WordsPhrasesFoundInsertAfterSelection()
Dim myWords() 'Array of words and phrases
Dim strResults As String 'myWords found
Dim r As Range 'Create a Range object
Dim var As Variant
Dim numfound() As Integer 'Runs in myWords found counts
Dim idx As Integer 'Runs in myWords found counts
Dim MyRange As Object 'Runs for the whole program
Set MyRange = Selection.Range 'myWords found listed after the Selection
Dim j As Long
myWords = Array("through", "this project", "ups and downs")
ReDim numfound(0 To UBound(myWords))
For Each r In Selection.Words
MatchAllWordForms = True
idx = 0
For Each var In myWords
If Trim(r.text) = myWords(idx) Then
numfound(idx) = numfound(idx) + 1
End If
idx = idx + 1
Next var
Next r
idx = 0
For Each var In myWords
If numfound(idx) > 0 Then
strResults = strResults & myWords(idx) & ", "
End If
idx = idx + 1
Next var
MyRange.Collapse Direction:=wdCollapseEnd
If strResults = "" Then GoTo ErrorHandler
MyRange.InsertAfter text:= _
"(myWords found: "
strResults = Left(strResults, Len(strResults) - 2)
MyRange.InsertAfter strResults
MyRange.InsertAfter text:= _
")"
ErrorHandler:
With MyRange
.Font.Name = "Arial"
.Font.Size = 11
.Font.Bold = False
.Font.Underline = False
.ParagraphFormat.Alignment = ppAlignLeft
.ParagraphFormat.LeftIndent = 0
.Font.ColorIndex = 1 'black
.ParagraphFormat.SpaceBefore = 0
.ParagraphFormat.SpaceBeforeAuto = False
.ParagraphFormat.SpaceAfter = 0
.ParagraphFormat.SpaceAfterAuto = False
.ParagraphFormat.LineSpacingRule = wdLineSpaceSingle
.ParagraphFormat.LineUnitBefore = 0
.ParagraphFormat.LineUnitAfter = 0
Options.DefaultHighlightColorIndex = wdNoHighlight
End With
MyRange.InsertAfter (Chr(11))
MyRange.InsertBreak Type:=wdSectionBreakContinuous
End Sub