开发者

Problem with format a single excel column with OLE automation using Delphi

开发者 https://www.devze.com 2022-12-30 10:44 出处:网络
I have piece of code which I use to format a range of cells in Excel. It works fine in Excel 2007 but when the range is only 1 column wide and it is Excel 2003 instead of 2007, I\'ll get an error sayi

I have piece of code which I use to format a range of cells in Excel. It works fine in Excel 2007 but when the range is only 1 column wide and it is Excel 2003 instead of 2007, I'll get an error saying the I am assigning invalid value for a border's line style.

** valuables such as "xlInsideHorizontal", I have declared them as CONSTANT with the proper values.

Please help.

   procedure formatCells(FRCELLROW, FRCELLCOL, TOCELLROW, TOCELLCOL: Integer;
                         TOPSTYLE, TOPCOLOUR, TOPWEIGHT,
                         BOTTOMSTYLE, BOTTOMCOLOUR, BOTTOMWEIGHT,
                         LEFTSTYLE, LEFTCOLOUR, LEFTWEIGHT,
                         RIGHTSTYLE, RIGHTCOLOUR, RIGHTWEIGHT: Integer;
                         INNERVSTYLE, INNERVCOLOUR, INNERVWEIGHT: Integer;
                         INNERHSTYLE, INNERHCOLOUR, INNERHWEIGHT: Integer;
                         HORIZONTALCELLALIGNMENT: Integer;
                         FontBold: Boolean;
                         NumberFormat: String
                         );
   var
      tmpRange: Variant;
   begin
        tmpRange := eclApp.range[eclApp.Cells[FRCELLROW, FRCELLCOL],
                                 eclApp.Cells[TOCELLROW, TOCELLCOL]];

        tmpRange.Borders[xlEdgeTop].LineStyle := TOPSTYLE;
        if TOPSTYLE <> xlNone then begin
           tmpRange.Borders[xlEdgeTop].ColorIndex := TOPCOLOUR;
           tmpRange.Borders[xlEdgeTop].Weight := TOPWEIGHT;
        end; //if
        tmpRange.Borders[xlEdgeBottom].LineStyle := BOTTOMSTYLE;
        if BOTTOMSTYLE <> xlNone then begin
           tmpRange.Borders[xlEdgeBottom].ColorIndex := BOTTOMCOLOUR;
           tmpRange.Borders[xlEdgeBottom].Weight := BOTTOMWEIGHT;
        end; //if
        tmpRange.Borders[xlEdgeLeft].LineStyle := LEFTSTYLE;
        if LEFTSTYLE <> xlNone then begin
           tmpRange.Borders[xlEdgeLeft].ColorIndex := LEFTCOLOUR;
           tmpRange.Borders[xlEdgeLeft].Weight := LEFTWEIGHT;
        end; //if
        tmpRange.Borders[xlEdgeRight].LineStyle := RIGHTSTYLE;
        if RIGHTSTYLE <> xlNone then begin
           tmpRange.Borders[xlEdgeRight].ColorIndex := RIGHTCOLOUR;
           tmpRange.Borders[xlEdgeRight].Weight := RIGHTWEIGHT;
        end; //if
        tmpRange.Borders[xlInsideVertical].LineStyle := INNERVSTYLE;
        if INNERVSTYLE <> xlNone then begin
           tmpRange.Borders[xlInsideVertical].ColorIndex := INNERVCOLOUR;
           tmpRange.Borders[xlInsideVertic开发者_高级运维al].Weight := INNERVWEIGHT;
        end; //if
        tmpRange.Borders[xlInsideHorizontal].LineStyle := INNERHSTYLE;
        if INNERHSTYLE <> xlNone then begin
           tmpRange.Borders[xlInsideHorizontal].ColorIndex := INNERHCOLOUR;
           tmpRange.Borders[xlInsideHorizontal].Weight := INNERHWEIGHT;
        end; //if
        tmpRange.HorizontalAlignment := HORIZONTALCELLALIGNMENT;
        tmpRange.Font.Bold := FontBold;
        tmpRange.NumberFormat := NumberFormat;
   end; //


Likewise, if you have specified a "1" row height, you'd get the same error. Inner styles are for lines between adjacent cells. A "1" column width range have no horizontally adjacent cells, hence no vertical inner lines. Excel 2007 is probably ignoring the invalid value, whereas Excel 2003 is throwing an error.

Test for the number of columns and number of rows for ranges before passing values to your "formatCells" procedure, and if you encounter a "1" in any of these pass "xlNone" (-4142) in place of "INNERVSTYLE, INNERVCOLOUR, INNERVWEIGHT" or "INNERHSTYLE, INNERHCOLOUR, INNERHWEIGHT".

0

精彩评论

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