VB绘制Excel时网格线异常的情况
该VB程序大致如下。
我绘制网格线的方法是,通过range.select选中一个区域,然后绘制这个区域的网格线。这段程序第一次运行是正常的,绘制的表格中,网格线都正确,但第二次及以后的运行中,却画不出网格,而且有时会报错误“1004,不能设置类Border的LineStyle属性”。我查了一下,1004的错误含义是在绘制选定区域内部的交叉线时,发现只有一行或一列,因此不存在交叉,但我这里选中的区域是"A5:D9",是多行多列。
为什么前后两次运行会有这种差异呢?
'----------------------------------------------------------
Dim xlAppX As New Excel.Application
Dim xlBookX As New Excel.Workbook
Set xlAppX = CreateObject("Excel.Application")
Set xlBookX = xlAppX.Workbooks.Open(App.Path & "\SystemParaList.xlt")
Set xlBookX = xlAppX.ActiveWorkbook
xlAppX.Application.Visible = True
Dim xlSheet As New Excel.Worksheet
Set xlSheet = xlBookX.Sheets(2)
xlSheet.Select
xlSheet.Range("A5:D9").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThick
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThick
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThick
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThick
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
'---------<
Set xlSheet = Nothing
xlAppX.Application.Visible = True
Set xlBookX = Nothing
Set xlAppX = Nothing