Reputation: 45
I'm writing a VSTO Add-In for Excel 2013/2016 in C#.
The add-in needs to mark a specific cell in an Excel worksheet somehow. (This mark needs to survive the closing of the workbook.) Then the Add-In needs to insert data at this specific cell. Therefore I need to get the position/range of this cell.
What is the best way to mark a cell and how do i get the position of that marked cell?
Is there a better solution than searching every cell for a keyword like "&?%insertHere" ?
Upvotes: 2
Views: 346
Reputation: 45
One way to mark a cell is naming it:
// Name a cell (here: the active cell in the active Excel sheet)
Excel.Range cell = Globals.ThisAddIn.Application.ActiveCell;
cell.Name = "MyMarkedCell";
// Get a cell by its name
Worksheet sheet = Globals.ThisAddIn.Application.ActiveSheet;
Excel.Range cell = sheet.get_Range("MyMarkedCell");
// Careful: get_Range throws an Exception, when there is no cell with the given name.
Upvotes: 2
Reputation: 3195
You could insert a comment into the cell. Then loop the used range per sheet looking for the comment.
Excel.Range cell;
cell.AddComment("Insert Here");
Upvotes: 2