0% found this document useful (0 votes)
5 views4 pages

CBA Code

This VBA macro cleans comments in a Word document by removing unwanted words while preserving hyperlinks and formatting. It iterates through comments, deletes those that consist solely of unwanted words, and removes unwanted words from the start of other comments. Additionally, it counts the number of comments by each author and displays a report in a message box.

Uploaded by

khoitran2861
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

CBA Code

This VBA macro cleans comments in a Word document by removing unwanted words while preserving hyperlinks and formatting. It iterates through comments, deletes those that consist solely of unwanted words, and removes unwanted words from the start of other comments. Additionally, it counts the number of comments by each author and displays a report in a message box.

Uploaded by

khoitran2861
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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

You might also like