I have a list of keywords I need to verify are included in hundreds of documents (resumes) I'm reviewing. I figured out how to write a macro to highlight/color the keywords from the list which are FOUND within the document (code below) but I need to know which keywords are MISSING from the document. Is there a way to do this in a macro in Word? I will send you a gift if you help me!!! I'm desperate.
Sub HighlightWords()
Dim vWords As Variant
Dim sWord As Variant
vWords = Array("SQL query", "Selenium", "Cucumber", "Rest-Assured", "Rest assured", "REST API", "TestNG", "SVN", "Subversion", "Maven", "IntelliJ", "Ecliipse", "Confluence", "JIRA", "Sauce Labs", "GitLab", "HTML", "XPATH", "CSS", "Object Oriented Programming", "Object-Orienting Programming", "OOP")
For Each sWord In vWords
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = wdYellow
With Selection.Find
.Text = sWord
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Next sWord
End Sub
CodePudding user response:
The following code will display the missing terms in a message box.
Sub HighlightWords()
Dim vWords As Variant
Dim sWord As Variant
Dim Missing As String
vWords = Array("SQL query", "Selenium", "Cucumber", "Rest-Assured", "Rest assured", "REST API", "TestNG", "SVN", "Subversion", "Maven", "IntelliJ", "Ecliipse", "Confluence", "JIRA", "Sauce Labs", "GitLab", "HTML", "XPATH", "CSS", "Object Oriented Programming", "Object-Orienting Programming", "OOP")
For Each sWord In vWords
With ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Highlight = wdYellow
.Text = sWord
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
If Not .Execute(Replace:=wdReplaceAll) Then Missing = Missing & sWord & ", "
End With
Next sWord
MsgBox "Missing terms:" & vbCr & vbCr & Left(Missing, Len(Missing) - 2)
End Sub
CodePudding user response:
The following will give you a list of the missing words as collection or array:
Option Explicit
Public Sub FindMissingWords()
Dim vWords() As Variant
vWords = Array("SQL query", "Selenium", "Cucumber", "Rest-Assured", "Rest assured", "REST API", "TestNG", "SVN", "Subversion", "Maven", "IntelliJ", "Ecliipse", "Confluence", "JIRA", "Sauce Labs", "GitLab", "HTML", "XPATH", "CSS", "Object Oriented Programming", "Object-Orienting Programming", "OOP")
Dim MissingWords As New Collection
Dim sWord As Variant
For Each sWord In vWords
If Not IsKeywordInRange(sWord, ThisDocument.Range) Then
MissingWords.Add sWord
End If
Next sWord
If MissingWords.Count > 0 Then
MsgBox "The following " & MissingWords.Count & " words are missing: " & Join(CollectionToArray(MissingWords), ", "), vbExclamation
Else
MsgBox "No words are missing.", vbInformation
End If
End Sub
Public Function IsKeywordInRange(ByVal Keyword As String, ByVal InRange As Range) As Boolean
'check if a keyword is in the range
With InRange.Find
.Text = Keyword
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
IsKeywordInRange = InRange.Find.Execute
End Function
Public Function CollectionToArray(ByVal Coll As Collection, Optional ByVal StartIdx As Long, Optional ByVal Size As Long) As Variant
' Convert a collection into an array
Dim Arr() As Variant
Dim Ci As Variant ' Collection element
Dim i As Long
' Make sure Size is not less than collection size
If Size < Coll.Count Then Size = Coll.Count
' Transfer collection to array
ReDim Arr(StartIdx To StartIdx Size - 1)
For Each Ci In Coll
Arr(i) = Ci
i = i 1
Next
CollectionToArray = Arr
End Function
Note that outsourcing the test if a keyword is in the document into a function makes your code more readable and it is easier to test or re-use.