通过仍在该过程中运行的vba代码关闭Excel工作簿

时间:2014-02-26 20:04:11

标签: vba access-vba

通过Access VBA我打开工作簿并使visible = False,然后我做了一些事情并关闭。但它仍然在 Taskmanager 中运行。

     Dim ExcelApp As New Excel.Application
     Dim ExcelBook As New Excel.Workbook
     Dim rng As Excel.Range
     Dim rngDefine As Excel.Range

     Set ExcelBook = ExcelApp.Workbooks.Open("C\temp\find.xlsm")
     ExcelApp.Visible = False       

     '//Do something

    ExcelApp.Quit
    ExcelBook.Close SaveChanges:=False
    Set ExcelBook = Nothing
    Set ExcelApp = Nothing         

1 个答案:

答案 0 :(得分:2)

上述评论者是正确的,您的新工作簿与您的ExcelApp实例无关。所以最后你关闭了你的新ExcelApp实例并关闭了你创建的新工作簿,但我认为新的工作簿是在一个新的Excel实例中打开...也许,所以你仍然会看到一个任务管理器中的孤立Excel进程。我会将您的代码更改为更像这样的内容:

Dim ExcelApp as Excel.Application
Dim ExcelBook as Excel.Workbook
Dim rng As Excel.Range
Dim rngDefine As Excel.Range
Set ExcelApp = New Excel.Application
Set ExcelBook = ExcelApp.Workbooks.Open("C\temp\find.xlsm")
'ExcelApp.Visible = False   You could maybe comment this out unless you use ".Activate" somewhere.  It should be the default anyway.
...
'At the end:
ExcelApp.DisplayAlerts = False
ExcelBook.Close
ExcelApp.DisplayAlerts = True
ExcelApp.Quit
'If right before "End Sub" the next two lines are of arguable necesity, but likely good practice
Set ExcelBook = Nothing
Set ExcelApp = Nothing