VB如何关闭Excel进程
如下代码检测Excel工作薄的记录数,但运行结束后Excel进程还在任务管理器显示。请教如何才能完全关闭Excel进程。
Dim xlApp As New Excel.Application, pTable As String
Private Sub Command1_Click()
On Error GoTo ErrHandle:
Set xlApp = CreateObject( "Excel.Application ")
xlApp.Visible = False
Dim i As Integer
xlApp.Workbooks.Open ( "f:\工作表.xls ")
Dim conn As New ADODB.Connection, rs As New ADODB.Recordset
Dim strSQL As String
'获取工作薄名称
For i = 1 To xlApp.Sheets.Count
pTable = xlApp.Sheets(i).Name
strSQL = "SELECT * FROM [ " + pTable + "$] "
conn.ConnectionString = "Driver={Microsoft Excel Driver _(*.xls)}; " & "DriverId=790; DBQ=F:\工作表.xls; "
conn.CursorLocation = adUseClient
conn.Open
Set rs = conn.Execute(strSQL)
conn.Close
Set rs = Nothing
msgbox CStr(rs.RecordCount)
Next
xlApp.Workbooks.Close
xlApp.Quit
Set xlApp = Nothing
If Not (xlApp Is Nothing) Then
xlApp.Quit
Set xlApp = Nothing
End If
MsgBox "计算完成! "
Exit Sub
ErrHandle:
MsgBox CStr(Err.Number) + Err.Description
End Sub
[解决办法]
可能是错误跳转后没有执行quit
ErrHandle:
xlApp.Quit
Set xlApp = Nothing
MsgBox CStr(Err.Number) + Err.Description
[解决办法]
Set rs = Nothing
msgbox CStr(rs.RecordCount)
后面这句有问题吧,rs都Nothing了,怎么还能取记录数。