Introduction
Generally, Comments are used to add some additional information associated with the cells in a worksheet. We use them every now and then for our requirement, we delete them when we do not need them any longer. The comments are useful if you need to document a particular value or to help you remember what a formula does. When you move the mouse pointer over a cell that has a comment, the comment pops up in a small box. We carry out the task using VSTO and Aspose.Cells for .NET independently.
Task Category: MS Excel.
Inserting a Comment
Task Description:
Open an existing excel file, add comment to a cell and save the excel file.
Source Codes:
Following are the parallel code snippets for VSTO (C#, VB) and Aspose.Cells for .NET (C#, VB).
1) VSTO
[C#]
…….
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using System.Reflection;
…….
//Instantiate the Application object.
Excel.Application excelApp = new Excel.ApplicationClass();
//Specify the template excel file path.
string myPath=@"d:\test\Book1.xls";
//Open the excel file.
excelApp.Workbooks.Open(myPath, Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
//Get the A1 cell.
Excel.Range rng1=excelApp.get_Range("A1", Missing.Value);
//Add the comment with text.
rng1.AddComment("This is my comment");
//Save the file.
excelApp.ActiveWorkbook.Save();
//Quit the Application.
excelApp.Quit();
[VB]
…….
Imports Microsoft.VisualStudio.Tools.Applications.Runtime
Imports Excel = Microsoft.Office.Interop.Excel
Imports Office = Microsoft.Office.Core
Imports System.Reflection
…….
'Instantiate the Application object.
Dim excelApp As Excel.Application = New Excel.ApplicationClass()
'Specify the template excel file path.
Dim myPath As String="d:\test\Book1.xls"
'Open the excel file.
excelApp.Workbooks.Open(myPath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value)
'Get the A1 cell.
Dim rng1 As Excel.Range=excelApp.Range("A1", Missing.Value)
'Add the comment with text.
rng1.AddComment("This is my comment")
'Save the file.
excelApp.ActiveWorkbook.Save()
'Quit the Application.
excelApp.Quit()
2) Aspose.Cells for .NET
[C#]
…….
using Aspose.Cells;
…….
//Instantiate a new Workbook.
Workbook workbook = new Workbook();
//Specify the template excel file path.
string myPath=@"d:\test\Book1.xls";
//Open the excel file.
workbook.Open(myPath);
//Add a Comment to A1 cell.
int commentIndex=workbook.Worksheets[0].Comments.Add("A1");
//Accessing the newly added comment
Comment comment=workbook.Worksheets[0].Comments[commentIndex];
//Setting the comment note
comment.Note="This is my comment";
//Save As the excel file.
workbook.Save(@"d:\test\Book1.xls");
[VB]
…….
Imports Aspose.Cells
…….
'Instantiate a new Workbook.
Dim workbook As Workbook = New Workbook()
'Specify the template excel file path.
Dim myPath As String="d:\test\Book1.xls"
'Open the excel file.
workbook.Open(myPath)
'Add a Comment to A1 cell.
Dim commentIndex As Integer=workbook.Worksheets(0).Comments.Add("A1")
'Accessing the newly added comment
Dim comment As Comment=workbook.Worksheets(0).Comments(commentIndex)
'Setting the comment note
comment.Note="This is my comment"
'Save As the excel file.
workbook.Save("d:\test\Book1.xls")
Removing the Comment
To remove a cell comment, use the following lines of code for VSTO (C#, VB) and Aspose.Cells for .NET (C#, VB).
1) VSTO
[C#]
//Remove the comment.
rng1.Comment.Delete();
[VB]
'Remove the comment.
rng1.Comment.Delete()
2) Aspose.Cells for .NET
[C#]
//Remove the comment.
workbook.Worksheets[0].Comments.RemoveAt("A1");
[VB]
'Remove the comment.
workbook.Worksheets(0).Comments.RemoveAt("A1")