开发者

Excel Range.BorderAround(), Border is always black

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.C开发者_如何学Pythonolor.FromArgb(178, 178, 178)));

The border color is always black no matter what RGB value I provide.


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));


worksheet.Cells[8, i].Borders.Color = 
    System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); 


.Range("H1:J1").BorderAround LineStyle:=xlContinuous, Weight:=xlMedium, color:=RGB(130, 130, 130)


range.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;

range.Borders.Color = System.Drawing.ColorTranslator.ToOle(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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜