user2316116
user2316116

Reputation: 6814

Copy Excel cell comments in a specified range

What is the way to copy all cell comments (on right click - Insert Comments) in a specified range?

Range r1 = (Range)ws1.get_Range("A1", "C10");
Range r2 = (Range)ws2.get_Range("A1", "C10");
r2.Value = r1.Value; // copies cell values and ignores comments

I know that r1.Copy(r2); would copy values and comments, but it shows unnecessary Excel dialogs due to validation issues and therefore I cannot use it.

Upvotes: 1

Views: 220

Answers (1)

UndeadBob
UndeadBob

Reputation: 1129

There's a AddComment method for Range. Unfortunately, it cannot be applied to a range of cells. I guess they assumed: why would you want the same comment written multiple times? So you'll have to loop:

for (int r = 1; r <= r1.Rows.Count; r++)
{
    for (int c = 1; c <= r1.Columns.Count; c++)
    {
        r2[r, c].AddComment(r1.Comment);
    }
}

Upvotes: 1

Related Questions