关闭Access创建的Excel实例后剩余的Excel进程

时间:2019-05-14 09:39:35

标签: excel vba ms-access

我正在使用下面的代码从Access到Excel中导出SQL语句:-

Dim myrs        As DAO.Recordset ' Create a recordset to hold the data
Dim myExcel     As New Excel.Application 
Dim mySheet     As Excel.Worksheet

Set mySheet = myExcel.Workbooks.Add(1).Worksheets(1) 
Set myrs = CurrentDb.OpenRecordset("select...") ' 

With mySheet
    .Range("A2").CopyFromRecordset myrs
End With

myExcel.Visible = True
myExcel.WindowState = xlMaximized

Set mySheet = Nothing
Set myExcel = Nothing

一切正常。但是,当我关闭Excel时,Excel进程仍处于打开状态。有办法防止这种情况发生吗?

1 个答案:

答案 0 :(得分:1)

尝试一下-它对我有用:

Dim myrs        As DAO.Recordset ' Create a recordset to hold the data
Dim myExcel     As New Excel.Application
Dim myBook      As Excel.Workbook
Dim mySheet     As Excel.Worksheet

Set myBook = myExcel.Workbooks.Add(1)
Set mySheet = myBook.Worksheets(1)
Set myrs = CurrentDb.OpenRecordset("select...") '

With mySheet
    .Range("A2").CopyFromRecordset myrs
End With

myExcel.Visible = True
myExcel.WindowState = xlMaximized

手动关闭Excel时,Excel实例关闭。