Sub CleanCommentsPreserveHyperlinkAndReport()
Dim cmt As Comment
Dim authorCount As Object
Dim unwantedBaseWords As Variant
Dim baseWord As Variant
Dim txt As String, cleanTxt As String
Dim nextChar As String
Dim offset As Long
Dim i As Long
Dim isOnlyUnwanted As Boolean
Dim rng As Range
Dim report As String
Dim author As Variant
' Base list of unwanted words (lowercase, without punctuation)
unwantedBaseWords = Array("ok", "like", "likes", "okie")
Set authorCount = CreateObject("Scripting.Dictionary")
' Iterate comments from last to first
For i = ActiveDocument.Comments.Count To 1 Step -1
Set cmt = ActiveDocument.Comments(i)
' Get raw text and trim
txt = Replace(cmt.Range.Text, vbCr, "")
txt = Trim(txt)
' Prepare cleanTxt to check standalone unwanted word
cleanTxt = LCase(txt)
Do While Len(cleanTxt) > 0 And (Right(cleanTxt, 1) = "." _
Or Right(cleanTxt, 1) = "," Or Right(cleanTxt, 1) = "!")
cleanTxt = Left(cleanTxt, Len(cleanTxt) - 1)
Loop
cleanTxt = Trim(cleanTxt)
' If comment only contains one unwanted word, delete it
isOnlyUnwanted = False
For Each baseWord In unwantedBaseWords
If cleanTxt = baseWord Then
isOnlyUnwanted = True
Exit For
End If
Next baseWord
If isOnlyUnwanted Then
cmt.Delete
Else
' Otherwise, remove unwanted word at start + trailing
punctuation/space
For Each baseWord In unwantedBaseWords
If LCase(Left(txt, Len(baseWord))) = baseWord Then
If Len(txt) > Len(baseWord) Then
nextChar = Mid(txt, Len(baseWord) + 1, 1)
Else
nextChar = ""
End If
If nextChar = "." Or nextChar = "," Or nextChar = "!" _
Or nextChar = " " Or nextChar = vbTab Or nextChar = "" Then
' Calculate offset length to delete
offset = Len(baseWord)
If nextChar = "." Or nextChar = "," Or nextChar = "!" Then
offset = offset + 1
If Len(txt) >= offset + 1 Then
If Mid(txt, offset + 1, 1) = " " Or Mid(txt, offset + 1, 1) =
vbTab Then
offset = offset + 1
End If
End If
ElseIf nextChar = " " Or nextChar = vbTab Then
offset = offset + 1
End If
' Delete the range from start of comment
Set rng = cmt.Range.Duplicate
rng.End = rng.Start + offset
rng.Delete
Exit For
End If
End If
Next baseWord
' Change font to Inter (preserves hyperlinks and formatting)
cmt.Range.Font.Name = "Inter"
' Count remaining comments by author
If authorCount.Exists(cmt.Author) Then
authorCount(cmt.Author) = authorCount(cmt.Author) + 1
Else
authorCount.Add cmt.Author, 1
End If
End If
Next i
' Build and display report
report = "Comment count by author:" & vbCrLf
For Each author In authorCount.Keys
report = report & author & ": " & authorCount(author) & vbCrLf
Next author
MsgBox report, vbInformation, "Comment Report"
End Sub