This is the code I am using:
rngData.BorderAround(Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexNone, System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(178, 178, 178)));
The border color is always black no matter what RGB value I provide.
When you go to Format> Cells> Borders select the Color first, select a Style [if necessary] next, then select the Preset or use the Border buttons last. If you'd like to help remind Microsoft of how unintuitive it is you can submit your comments using Help> Send Feedback.
Go to the border tab and change the color from automatic and then you have to re-apply the border, it will not change automatically by just changing the color in the drop down. You still have to click and add the border.
On the Border tab, under Color, click the color that you want to apply, and then under Border, click the specific pieces of the cell border to apply the color to. Click OK.
I had the same problem, couldn't find any solution on the web, MS documentation for use of this method in VSTO is a bit poor.
Anyway, probably a bit late for you seeing as you posted months ago, but my workaround was simply to not use the Range.BorderAround method and write my own!
private void BorderAround(Excel.Range range, int colour) { Excel.Borders borders = range.Borders; borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Excel.XlLineStyle.xlContinuous; borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle = Excel.XlLineStyle.xlContinuous; borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Excel.XlLineStyle.xlContinuous; borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous; borders.Color = colour; borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlLineStyleNone; borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle = Excel.XlLineStyle.xlLineStyleNone; borders[Excel.XlBordersIndex.xlDiagonalUp].LineStyle = Excel.XlLineStyle.xlLineStyleNone; borders[Excel.XlBordersIndex.xlDiagonalDown].LineStyle = Excel.XlLineStyle.xlLineStyleNone; borders = null; }
Can be invoked as per below example (Contents_Table is a NamedRange in my worksheet):
BorderAround(Contents_Table.Cells, System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(79, 129, 189)));
Hope this helps someone else out there tearing their hair out.
Alternatively if you're not worried to ensure the removal of inside and diagonal lines I have successfully used:
range.Borders.LineStyle = Excel.XlLineStyle.xlContinuous; range.Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(153, 153, 153));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With