vb操作excel问题,急,请高手帮忙看看!

来源:百度知道 编辑:UC知道 时间:2024/09/22 14:30:58
这是我建的一个函数,代码是从EXCEL宏中拷贝来的。
现在问题是我在第一个窗体的过程中调用该函数,可以正确执行,但是换了一个窗体的过程来调用却提示“对象变量或WITH块变量未设置”,在上述两个过程中,我都已正确建立与EXCEL的联接,且调用第一个过程后EXCEL对象已关闭。
Public Function Set_Border(ByVal row_no1 As String, ByVal xlSheet_in As Excel.Worksheet) As Boolean

xlSheet_in.Activate
xlSheet_in.Range("A1:F" & row_no1).Select

Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selecti

当程序创建EXCEL对象后,EXCEL就一直存在,就算你对它关闭,它都一直存在

解决办法就是,在调用第一次运行EXCEL窗口的那个程序段运行一次
GC.Collect()
这样可以清除多余的进程
这时用其它窗口再一次调用应该没有问题了.