Hello, I am trying to create a macro that searches and highlights all the words in a document, including in the footnotes. The
list of words to highlight is taken from another file (multisearch.docx), and the words in the list are separated by Return/Enter key.
But the search/highlight function only works for the text-body and not for the footnotes. Can anybody tell me
how to modify the vba below to include the footnotes in the search/highlight macro?
------------------------------------------
Sub FindMultiItemsInDoc()
Dim objListDoc As Document
Dim objTargetDoc As Document
Dim objParaRange As Range, objFoundRange As Range
Dim objParagraph As Paragraph
Set objTargetDoc = ActiveDocument
Set objListDoc = Documents.Open(FileName:="C:\multisearch.docx")
objTargetDoc.Activate
For Each objParagraph In objListDoc.Paragraphs
Set objParaRange = objParagraph.Range
objParaRange.End = objParaRange.End - 1
With Selection
.HomeKey Unit:=wdStory
' Find target items.
With Selection.Find
.ClearFormatting
.Text = objParaRange
.MatchWholeWord = True
.MatchCase = False
.Execute
End With
' Highlight the found items.
Do While .Find.Found
Set objFoundRange = Selection.Range
objFoundRange.HighlightColorIndex = wdBrightGreen
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
Next objParagraph
End Sub
--------------------------------------------------
Alternatively, i tried to modify this VBA file found here (https://www.office-forums.com/threads/multiple-search-replace-also-in-footnotes.1869777/), but with no succes.
---------------------------------------------------
Public Sub MultiWordFindReplace()
Dim rngstory As Word.Range
Dim ListArray
Dim WordList As Document
Dim i As Long
Set WordList = Documents.Open(FileName:="C:\SR.doc")
ListArray = WordList.Tables(1).Range.Text
ListArray = Split(ListArray, Chr(13) & Chr(7))
WordList.Close
'Fix the skipped blank Header/Footer problem
MakeHFValid
'Iterate through all story types in the current document
For Each rngstory In ActiveDocument.StoryRanges
'Iterate through all linked stories
Do
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
For i = LBound(ListArray) To UBound(ListArray) - 1 Step 3
With rngstory.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ListArray(i)
.Replacement.Text = ListArray(i + 1)
.MatchWholeWord = True
.Execute Replace:=wdReplaceAll
End With
Next i
Set rngstory = rngstory.NextStoryRange
Loop Until rngstory Is Nothing
Next
End Sub
Public Sub MakeHFValid()
'And this too
Dim lngJunk As Long
' It does not matter whether we access the Headers or Footers property.
' The critical part is accessing the stories range object
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
End Sub
----------------------------------------------------------
Any help would be appreciated.
list of words to highlight is taken from another file (multisearch.docx), and the words in the list are separated by Return/Enter key.
But the search/highlight function only works for the text-body and not for the footnotes. Can anybody tell me
how to modify the vba below to include the footnotes in the search/highlight macro?
------------------------------------------
Sub FindMultiItemsInDoc()
Dim objListDoc As Document
Dim objTargetDoc As Document
Dim objParaRange As Range, objFoundRange As Range
Dim objParagraph As Paragraph
Set objTargetDoc = ActiveDocument
Set objListDoc = Documents.Open(FileName:="C:\multisearch.docx")
objTargetDoc.Activate
For Each objParagraph In objListDoc.Paragraphs
Set objParaRange = objParagraph.Range
objParaRange.End = objParaRange.End - 1
With Selection
.HomeKey Unit:=wdStory
' Find target items.
With Selection.Find
.ClearFormatting
.Text = objParaRange
.MatchWholeWord = True
.MatchCase = False
.Execute
End With
' Highlight the found items.
Do While .Find.Found
Set objFoundRange = Selection.Range
objFoundRange.HighlightColorIndex = wdBrightGreen
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
Next objParagraph
End Sub
--------------------------------------------------
Alternatively, i tried to modify this VBA file found here (https://www.office-forums.com/threads/multiple-search-replace-also-in-footnotes.1869777/), but with no succes.
---------------------------------------------------
Public Sub MultiWordFindReplace()
Dim rngstory As Word.Range
Dim ListArray
Dim WordList As Document
Dim i As Long
Set WordList = Documents.Open(FileName:="C:\SR.doc")
ListArray = WordList.Tables(1).Range.Text
ListArray = Split(ListArray, Chr(13) & Chr(7))
WordList.Close
'Fix the skipped blank Header/Footer problem
MakeHFValid
'Iterate through all story types in the current document
For Each rngstory In ActiveDocument.StoryRanges
'Iterate through all linked stories
Do
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
For i = LBound(ListArray) To UBound(ListArray) - 1 Step 3
With rngstory.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ListArray(i)
.Replacement.Text = ListArray(i + 1)
.MatchWholeWord = True
.Execute Replace:=wdReplaceAll
End With
Next i
Set rngstory = rngstory.NextStoryRange
Loop Until rngstory Is Nothing
Next
End Sub
Public Sub MakeHFValid()
'And this too
Dim lngJunk As Long
' It does not matter whether we access the Headers or Footers property.
' The critical part is accessing the stories range object
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
End Sub
----------------------------------------------------------
Any help would be appreciated.