So for some purpose, I need to insert a comment in the last column of excel.
Since I don’t want user to be able to see the comment I inserted, I try to hide the column.
Somehow it gave me an error “Cannot shift objects off sheet”
Constants.cs
HIDDEN_DATA_COL = 16384; // Which is last column in excel 2007
MyUtil.cs
Excel.Range range = ws.get_Range(ws.Cells[1, Constants.HIDDEN_DATA_COL], ws.Cells[1, Constants.HIDDEN_DATA_COL]);
range.AddComment(result);
range.Comment.Shape.Width = 50;
range.Comment.Shape.Height = 50;
range.EntireColumn.Hidden = true;
I also make some trial :
- remove range.EntireColumn.Hidden and the code is OK.
- changing the HIDDEN_DATA_COL to number less than 10 its also make the runtime error dissapear.
Any idea on how to fix it?
“Hiding” comments in the last column of the sheet is probably not the best way to solve your original issue.
You faced the issue of the Cannot shift objects off sheet error within your code but even if you managed to do it, your user would face it whenever he or she would try to hide or insert any column.
Here is how Microsoft advice to handle this: http://support.microsoft.com/kb/211769
As this might be a case of an XY issue, I will suggest some workarounds.
Actually, in your case, you’d probably hide your comment somewhere else, in any hidden cell for instance and if you want the user not to see it, just protect your sheet from unhiding cells.
If you don’t want to protect, you can store your comments in another sheet and hide it with VBA using the
xlVeryHidden property(user cannot unhide the sheet without VBA).