How to: Programmatically Add and Delete Worksheet Comments

You can programmatically add and delete comments in Microsoft Office Excel worksheets. Comments can be added only to single cells, not to multi-cell ranges.

Applies to: The information in this topic applies to document-level projects and application-level projects for Excel 2013 and Excel 2010. For more information, see Features Available by Office Application and Project Type.

The following examples assume that there is a single-cell NamedRange control named dateComment on a worksheet named Sheet1.

To add a new comment to a named range

  • Call the AddComment method of the NamedRange control and supply the comment text. This code must be placed in the Sheet1 class.

    this.dateComment.AddComment("Comment added " + DateTime.Now.ToString());
    

To delete a comment from a named range

  • Verify that a comment exists on the range and delete it. This code must be placed in the Sheet1 class.

    if (this.dateComment.Comment != null)
    {
        this.dateComment.Comment.Delete();
    }
    

The following examples assume that there is a single-cell Microsoft.Office.Interop.Excel.Range named dateComment on the active worksheet.

To add a new comment to an Excel range

To delete a comment from an Excel range

  • Verify that a comment exists on the range and delete it.

    Excel.Range dateComment = this.Application.get_Range("A1");
    if (dateComment.Comment != null)
    {
        dateComment.Comment.Delete();
    }
    
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft