Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel Range.BorderAround(), Border is always black

Tags:

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.

like image 267
Aseem Gautam Avatar asked Feb 15 '10 10:02

Aseem Gautam


People also ask

Why are my Excel borders black?

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.

Why won't Excel let me change the border color?

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.

How do I change the color of borders in Excel?

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.


2 Answers

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.

like image 175
Wad Avatar answered Sep 29 '22 10:09

Wad


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)); 
like image 21
Barry Kaye Avatar answered Sep 29 '22 11:09

Barry Kaye