Microsoft Word Comment a Complete Reference with VBA

Microsoft Word Comment a Complete Reference with VBA

Comment

Helps user to describe own thought in the document in a separate workspace so others can easily understand the intention of the author. Comment in a Word document refers as an object which is exposed by Comments collection. Using VBA (Visual Basic for Applications) user can iterate each comment by a for each loop or by passing an Index (a positive integer). Let’s understand various properties and methods exposed by comment object in Microsoft Word using VBA code.

Insert comment

  1. Select the range or text in the document where you wish to insert a comment
  2. Write click and choose New Comment command from the popup or navigate to Insert tab and click on Comment button under Comments group
  3. A floating bar would be displayed at the right side of the document with blinking cursor where you can type your text

Add Method

Public Sub CommentAddMethodExample()
    'Declare variable to hold reference of active document
    Dim oDoc As Document
    
    'bind active document reference
    Set oDoc = ActiveDocument
    
    'Declare range variable
    Dim oRange As Range
    
    'Bind Selection to range
    Set oRange = Selection.Range
    
    'Add comment to selection
    oDoc.Comments.Add Range:=oRange, Text:="VBAOVERALL offers a complete solution"
    
    'Memory cleanup
    Set oRange = Nothing
    Set oDoc = Nothing
End Sub

Output

Author Properties

Public Sub CommentAuthorChangeExample()
    'Declare variable to hold reference of active document
    Dim oDoc As Document
    
    'bind active document reference
    Set oDoc = ActiveDocument
    
    'Change author
    oDoc.Comments(1).Author = "INFOEXTRACT"
    
    'Memory cleanup
    Set oDoc = Nothing
End Sub

Output

Count Property

Public Sub CommentsCountExample()
    'Declare variable to hold reference of active document
    Dim oDoc As Document
    
    'bind active document reference
    Set oDoc = ActiveDocument
    
    'object to comments collection
    Dim oComments As Comments
    
    'Bind all comments
    Set oComments = oDoc.Comments
    
    'Print total comments in the document
    Debug.Print "There are total "; oComments.Count & " comments in " & oDoc.Name & " document"
    
    'Memory cleanup
    Set oComments = Nothing
    Set oDoc = Nothing
End Sub

Output

There are total 1 comments in OVERALL.docm document

ShowBy Property

This property works like a filter which filters the comment and displays in a split view. Consider following document where we have multiple comments with different author and we want to see summary of comments made by “VBAOVERALL” author.

Code example

Public Sub ShowBycommentsExample()
    'Declare variable to hold reference of active document
    Dim oDoc As Document
    
    'bind active document reference
    Set oDoc = ActiveDocument
    
    'object to comments collection
    Dim oComments As Comments
    
    'Bind all comments
    Set oComments = oDoc.Comments
    
    'Split view to show comments
    oDoc.ActiveWindow.View.SplitSpecial = wdPaneComments
    
    'Show by
    oComments.ShowBy = "VBAOVERALL"
    
    'Memory cleanup
    Set oComments = Nothing
    Set oDoc = Nothing
End Sub

Output

Comment Range

Public Sub ReadCommentsExample()
    'Declare variable to hold reference of active document
    Dim oDoc As Document
    
    'bind active document reference
    Set oDoc = ActiveDocument
    
    'object to comments collection
    Dim oComments As Comments
    
    'Bind all comments
    Set oComments = oDoc.Comments
    
    Dim oComment As Comment
    
    'Print all comments
    For Each oComment In oComments
        'Print each comment and its author
        Debug.Print "Comment : " & oComment.Range.Text & " Written by : " & oComment.Author & " Created on : " & oComment.Date
    Next oComment
    
    'Memory cleanup
    Set oComments = Nothing
    Set oComment = Nothing
    Set oDoc = Nothing
End Sub

Output

Comment : VBAOVERALL offers a complete solution Written by : INFOEXTRACT Created on : 8/13/2020 1:42:00 PM
Comment : testing Written by : VBAOVERALL Created on : 8/13/2020 1:54:00 PM
Comment : another test Written by : VBAOVERALL Created on : 8/13/2020 1:57:00 PM
Comment : what a test? Written by : VBAOVERALL Created on : 8/13/2020 1:57:00 PM
Comment : Hallo vbaoverall testing Written by : INFOEXTRACT Created on : 8/13/2020 1:58:00 PM

DeleteRecursively

Public Sub DeleteCommentsExample()
    'Declare variable to hold reference of active document
    Dim oDoc As Document
    
    'bind active document reference
    Set oDoc = ActiveDocument
    
    'object to comments collection
    Dim oComments As Comments
    
    'Bind all comments
    Set oComments = oDoc.Comments
    
    Dim oComment As Comment
    
    'Iterate each comment
    For Each oComment In oComments
        'Delete all comments by VBAOVERALL
        If oComment.Author = "VBAOVERALL" Then
            oComment.DeleteRecursively
        End If
    Next oComment
    
    'Memory cleanup
    Set oComments = Nothing
    Set oComment = Nothing
    Set oDoc = Nothing
End Sub

Output

Please leave your comments or queries under comment section also please do subscribe to our blogs to keep your self upto date.

Leave a Reply

Your email address will not be published. Required fields are marked *