CopyStyle - Source Range Size

Hi,

I've searched your help and forums and don't see it written anywhere that for CopyStyle to work properly, the source range must be bigger than, or equal to, the destination range. I'm therefore wondering whether this behaviour is by design or unintentional. For example (in VB):

r = wks.Cells.CreateRange("a1:e1")
r2 = wks.Cells.CreateRange("a2:e6")

r2.CopyStyle(r)

Because the source range (r) is only 1 row, the formatting is only copied to the first row of the destination range (r2). The solution is to loop through each row of the destination range but that seems a little clumsy when Excel automation performs the action in one statement. If you could confirm whether this is intentional behaviour that would be appreciated.

Thanks, Glynn.

Hi,


I think you may try like following, kindly refer to the following sample code:
e.g
Sample code:

Workbook workbook = new Workbook();
workbook.Open(“e:\test\syle\template.xlsx”);
Cells cells = workbook.Worksheets[0].Cells;
Range range = cells.CreateRange(“B1:G1”);
Range dRange = cells.CreateRange(“B12:B17”);
PasteOptions options = new PasteOptions();
options.PasteType = PasteType.Values;
options.Transpose = true;
dRange.Copy(range, options);
workbook.Save(“e:\test2\out1.xlsx”);


Hope, this helps a bit.

Thank you.

Thanks Amjad. If the CopyStyle behaviour is intentional as you imply, I will simply loop through the destination rows as suggested in my question. I appreciate your fast response.

Hi,


Well, yes you may try your workaround to copy the formatting from source to destination range of cells. Also, you may try the following line instead if it works for you:
e.g
Sample code:

options.PasteType = PasteType.All;

Thank you.