hypehuman
hypehuman

Reputation: 1369

c# Excel Interop - Borders lost on multiple Range.Insert()s

When I call Range.Copy() on a column and then call Range.Insert() before another column, it works perfectly; the original column is duplicated to where I want it. However, if I make another call to Range.Insert() without calling Range.Copy() again, it does insert a new column where I want it, but without any borders.

I find that if I do call Range.Copy() every time, the output is exactly what I want, but it takes about 5 times as long. Do I really need to do that?

Here is my code for reference:

Range sheetCols = sheet.Columns;

...

int col = routesStartCol;
foreach (RouteObject route in loc.Routes)
{
    Range thisCol = sheetCols[col]; com.Got(thisCol);
    if (col == routesStartCol)
        thisCol.Copy();
    else
        thisCol.Insert(XlInsertShiftDirection.xlShiftToRight);
    sheetCells[routeNameRow, col] = route.Name;
    col++;
}

Upvotes: 1

Views: 1242

Answers (2)

hypehuman
hypehuman

Reputation: 1369

It looks like I really do need to copy every time. What it's really doing when you DON'T copy is just inserting a blank column, which by default copies some of the formatting of the column to the left.

Upvotes: 0

joy
joy

Reputation: 11

oRng1 = oSheet.get_Range("A1", "D1");
oRng2 = oSheet.get_Range("A2", "D2");
oRng1.Copy(oRng2); //copy r1 to r2
//to insert above r2
oRng2.Insert(Excel.XlInsertShiftDirection.xlShiftDown,Excel.XlInsertFormatOrigin.xlFormatFromLeftOrAbove);

Upvotes: 1

Related Questions