Excel .net How to insert or clear comment to an Excel cell programatically

By Miguel Santos

Our task is to: 1) Add a comment to the active cell 2) Clear a comment to the active cell using C#. The code shown here can be applied to an Excel range as well.

1) Add a comment to the active cell
//Get the application object
Excel.Application lApp = Globals.ThisAddIn.Application;
//Get the active cell object of the current sheet
Excel.Range myCell = lApp.ActiveCell as Excel.Range;
//Add comment
myCell.AddComment("textToAdd");

2) Clear a comment to the active cell
//Get the application object
Excel.Application lApp = Globals.ThisAddIn.Application;
//Get the active cell object of the current sheet
Excel.Range myCell = lApp.ActiveCell as Excel.Range;
//Add comment
myCell.ClearComments();

The sample code is presented in C# but it is easy to translate it to VB.NET.
How to build an Addin and explanation on Globals.ThisAddIn.Application can be found in  my article "Using VSTO Add-In to automate frequent Excel 2007 tasks"

Excel .net How to insert or clear comment to an Excel cell programatically  (4361 Views)