Macro for deleting specific comments

M

MClaire

Does anyone know how to create a macro to delete specific comments in at Word
2003 document? I'm looking for particularly to search by user (the person who
posted the comment) or by the content of the comment.

Thank you!
 
L

Lene Fredborg

If you include the following code in a macro and replace the XXX by the
desired initials, all comments inserted by the person with the specified
initials will be deleted (the initials appear in square brackets in the
comments):

Dim oCom As Comment

If ActiveDocument.Comments.Count = 0 Then
MsgBox "No comments found."
Exit Sub
End If

For Each oCom In ActiveDocument.Comments
'Delete all comments made by person with the specified initials
If oCom.Initial = "XXX" Then
oCom.Delete
End If
Next oCom

-----------------------
To delete all comments that contain a specified string in the comment text,
you can use the following code. Replace XXX by the text you want to find:

Dim oCom As Comment

If ActiveDocument.Comments.Count = 0 Then
MsgBox "No comments found."
Exit Sub
End If

For Each oCom In ActiveDocument.Comments
'Delete all comments that include the text specified in "XXX"
If InStr(1, oCom.Range.Text, "XXX") > 0 Then
oCom.Delete
End If
Next oCom

If you need to run such macro often, you could add an InputBox that asks
about the desired initials or text string so that you do not need to change
it in the macro. Please post back if this is relevant and if you need help on
the InputBox part.

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
M

MClaire

So, would that look like:

Dim oCom As Comment, Dim strData as String


strData = InputBox("Delete all comments from the user ...", "Enter User
Initials")


If ActiveDocument.Comments.Count = 0 Then
MsgBox "No comments found."
Exit Sub
End If

For Each oCom In ActiveDocument.Comments
'Delete all comments made by person with the specified initials
If oCom.Initial = strData Then
oCom.Delete
End If
Next oCom

Maybe?
 
L

Lene Fredborg

Try the version below:

Sub DeleteCommentsByInitials()
Dim oCom As Comment
Dim strData As String
Dim n As Integer

n = 0 'used to count deletions

If ActiveDocument.Comments.Count = 0 Then
MsgBox "No comments found."
Exit Sub
End If

Retry:
strData = InputBox("Delete all comments from the user ...", "Enter
UserInitials ")

If Len(strData) = 0 Then
If StrPtr(strData) = 0 Then
'Cancel clicked
Exit Sub
Else
'OK clicked, empty field
MsgBox "You must enter user initials. Please retry."
GoTo Retry
End If
Else
'Delete comments according to input
For Each oCom In ActiveDocument.Comments
If oCom.Initial = strData Then
oCom.Delete
n = n + 1
End If
Next oCom

End If

'Show msg with the result
MsgBox n & " comments deleted."

End Sub

------------
To create a corresponding macro for deleting comments containing a specific
string, you only need to replace the line:

If oCom.Initial = strData Then

with this line:

If InStr(1, oCom.Range.Text, strData) > 0 Then

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top