Excel Range.BorderAround(), Border is always black

Viewed 71610

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.

6 Answers

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));
worksheet.Cells[8, i].Borders.Color = 
    System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); 

To change the border color you have to use either

Color:=RGB(255, 0, 0)

with the RGB code you're interesting in, or ColorIndex:=3 - to get the red color, for instance.

If you use both, [ColorIndex:=3] will override [Color:=RGB(255, 0, 0)] - action visible when you try to set different colors for each, or use [colorindex:=xlColorIndeAutomatic] or with [xlColorIndexNone].

Unlike in Excel formulas, in VBA you can and probably should skip parameters as they are suggested by VBA's intellisense...

Related