Jaa


How to: Programmatically add comments to text in documents

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

The Comments property of the Document class adds a comment to a range of text in a Microsoft Office Word document.

Applies to: The information in this topic applies to document-level projects and VSTO Add-in projects for Word. For more information, see Features available by Office application and project type.

The following example adds a comment to the first paragraph in the document.

To add a new comment to text in a document-level customization

  1. Call the Add method of the Comments property and supply a range and the comment text. To use the following code example, run it from the ThisDocument class in your project.

    Me.Comments.Add(Me.Paragraphs(1).Range, "Add a comment to the first paragraph.")
    
    object text = "Add a comment to the first paragraph.";
    this.Comments.Add(this.Paragraphs[1].Range, ref text);
    

To add a new comment to text in a VSTO Add-in

  1. Call the Add method of the Comments property and supply a range and the comment text.

    The following code example adds a comment to the active document. To use this example, run it from the ThisAddIn class in your project.

    Me.Application.ActiveDocument.Comments.Add( _
        Me.Application.ActiveDocument.Paragraphs(1).Range, _
        "Add a comment to the first paragraph.")
    
    object text = "Add a comment to the first paragraph.";
    this.Application.ActiveDocument.Comments.Add(
        this.Application.ActiveDocument.Paragraphs[1].Range, ref text);
    

Robust programming

To change the user initials that Word adds to comments, use the UserInitials property.

See also