Export (0) Print
Expand All

May I Make a Comment?

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.
 

Lisa Wollin
Microsoft Corporation

May 2001

Applies to:
   Microsoft® Word 2000

Summary: Comments in Microsoft Word allow you to make notes and suggestions in a document without affecting the original text. This feature makes Word an especially powerful tool when working in a collaborative environment. The first part of this two-part article describes a custom solution for entering comments when certain words appear in your documents. (4 printed pages)


Download ODC_Comments-I.exe.

Contents

Introduction Understanding Comments Adding Comments to Specific Words in a Document
   Access the Data
   Return the Current Comments Collection for the Document
   Add Comment When Word Appears in Document Conclusion

Introduction

Comments in Microsoft Word 2000 are a powerful tool when several people are reviewing the same document. Using comments, reviewers can add notes and suggestions to a document. In the document, Word highlights commented text and, when you hover your mouse over the highlighted text, displays the comment in a ScreenTip.

While the tools for adding and reviewing comments in Word will satisfy the needs of most users, there are times when a Microsoft Office developer may want to create a custom commenting solution — for example, adding comments when certain words or phrases appear in a document, or reviewing comments for only two of several reviewers. Although Word 2000 does not allow you to hide the comments of one author while displaying others, you can create a custom solution that allows you to review the comments of multiple authors.

Part one of this article discusses how to add comments when certain words appear in a document. The second part of this two-part article will cover a solution for reviewing the comments of multiple authors.

However, before I show you this custom solution, I want to review some of the basics of working with comments in Word using Microsoft® Visual Basic® for Applications (VBA).

Understanding Comments

The Word 9.0 Object Library has two objects for working with comments: the Comment object and the Comments collection. You can use the Comments property of the Document, Selection, and Range objects to access the associated Comments collection. You use the properties and methods of the Comment object to work with individual comments in the Comments collection.

You add comments to a Document, Selection, or Range object using the Add method of the Comments collection. You remove comments from a Document, Selection, or Range object using the Delete method of the Comment object. The code below adds the specified comment to the currently selected text. If no text is currently selected, the code adds the specified comment at the insertion point.

Sub AddNewComment(ByVal strText As String)

   Selection.Comments.Add Range:=Selection.Range, Text:=strText

End Sub

Following is an example of how to use the above subroutine to add a comment to the text selected in the document or at the cursor position.

Sub CallAddNewComment()

   Call AddNewComment(strText:="This is a test comment.")

End Sub

Each Comment object in the Comments collection has several properties that you will use in the custom solutions in this article. The tables below describe these properties. There are additional members associated with the Comment object and Comments collection. For a complete list of members, see Word VBA Help or the Object Browser.

Table 1. Comment object properties

PropertyDescription
AuthorThe name of the user who entered the comment, as shown in the Name box on the User Information tab, Options dialog box.
InitialThe initials of the user who entered the comment, as shown in the Initials box on the User Information tab, Options dialog box. This property is limited to nine or fewer characters.
ReferenceThe reference mark displayed in a document where a comment appears. In Word 2000, this mark appears in a document as something like [LW1], which is a combination of the author's initials and the index number of the comment in the Comments collection for the entire document.
ScopeThe text, if any, associated with a comment.

Table 2. Comments collection properties

PropertyDescription
CountThe total number of comments in a Document, Selection, or Range object.
ShowByThe name of the author whose comments you want displayed. The default value of this property is "All Reviewers" which displays all comments. If you set the ShowBy property to the name of a specific author, then only comments made by that author are displayed in the comments pane of the document. If you set this property to "All Reviewers" (or a zero-length string), then all comments are displayed in the document's comments pane.

Adding Comments to Specific Words in a Document

The first custom solution enters comments into a document based on whether a sensitive word or phrase occurs in a document. The list of words is contained in a Microsoft® Access database table comprised of two fields: one for the sensitive word, and one for the associated comment that explains why the word is incorrect and how to correct it.

To accomplish this, the solution contains one subroutine and four functions:

  • MarkWords: The main subroutine that pulls the solution together.
  • GetData: Reads the records in the data source and returns an ActiveX Data Objects (ADO) recordset.
  • VerifyComments: Returns True if a sensitive word already has a WordCheck comment attached to it; otherwise, returns False.
  • ExistingComments: Returns a collection of existing comments that the VerifyComments function uses to check whether a sensitive word already has a comment attached to it.

Access the Data

To start, you need to open a connection to the database table that contains the list of words. To do so, you first need to add a reference to the Microsoft ActiveX Data Objects 2.5 Library (from the References dialog box, Tools menu). This allows you to use an ADO recordset to work with the records in the database. The GetData function requires the name of the database table to use and a connection string. Since ADO allows access to a variety of database formats, you can change the strConnection variable in the MarkWords subroutine to accommodate your choice of database.

Return the Current Comments Collection for the Document

The VerifyComments function checks to see whether a word from the database that is found in the document already has a comment attached to it, so the code needs to take a snapshot of the comments that are currently in the document. The ExistingComments function takes the name of the document and returns a VBA collection containing the comments. It is important to note that if the code used a Word Comments collection, the collection would change as the MarkWords subroutine added comments, which would cause the VerifyComments function to always return False regardless of whether a comment existed for the word.

Add Comment When Word Appears in Document

When the MarkWords subroutine finds in the document a word that exists in the database, it calls the VerifyComments function. If the VerifyComments function returns False, then the MarkWords subroutine adds a new comment for the found word.

For each word, the MarkWords subroutine searches through the entire document. However, every time a word is found, the search range narrows to include only the searched for text. Therefore, the MarkWords subroutine uses the following code to reset the Range object to include all text after the last found search string.

With rngSearch
   .Start = rngSearch.Start + Len(rngSearch.Text)
   .End = doc.Content.End
End With

Conclusion

The CheckWords solution included in this article could be used for a variety of purposes. I used it to locate words that might be politically or geographically sensitive. However, you might need to locate trademarks, acronyms, or special terminology in your documents. By simply providing a database table, you can adjust this solution to fit your needs.

Show:
© 2014 Microsoft