Excel.exe * 32不在任务pipe理器中closures。 从Access运行VBA。 Runing Office 2013

即使我在VBA中closures它,excel.exe * 32过程在Windows任务pipe理器中保持打开状态。 我从Access运行以下VBA代码。 我看了,并尝试各种解决scheme无济于事。 closuresexcel.exe的唯一方法是退出Access。 有人可以指出我缺less的东西吗?

Public Sub GenerateQualityReportsSub() On Error GoTo ERR_GenerateQualityReportsSub Dim xl As Excel.Application Dim wbk As Excel.Workbook Dim dbs As DAO.Database Dim rstRpt As DAO.Recordset Dim objMyRange As Object Dim rstList As DAO.Recordset Dim FullOutFileName As String Dim strSQLList As String Dim strSQLRpt As String Dim i As Integer Dim DiscrepancyRecords As Long Dim NeedToCloseExcel As Boolean Dim ReportName As String Dim col As Integer 'Initialize Variables Set dbs = CurrentDb RunDate = Now() FullOutFileName = "DataQualityDiscrepancyReport.xlsx" i = 0 DiscrepancyRecords = 0 NeedToCloseExcel = False 'Determine the Reports to Generate strSQLList = "" & _ "SELECT ReportNum, ReportName, SheetName, QueryName, [Responsible Department] " & _ "FROM [Data Quality Reports] " & _ "ORDER BY ReportNum" Set rstList = dbs.OpenRecordset(strSQLList, dbOpenSnapshot, dbReadOnly) If rstList.RecordCount = 0 Then i = 0 GoTo Exit_GenerateQualityReportsSub Else 'Open Excel Set xl = New Excel.Application 'Open the Excel File xl.Visible = True 'Make Excel Invisible to User 'Create the Excel Spreadsheet and Sheets Set wbk = xl.Workbooks.Add 'Add a Wookbook to the Excel File wbk.Sheets("Sheet1").Select 'Select Sheet 1 wbk.SaveAs FileName:=FullOutFileName 'Save the Excel File NeedToCloseExcel = True End If 'Create One Sheet Per Report i = 1 While Not rstList.EOF DiscrepancyRecords = 0 'Add, if necessary, and Rename the Sheet If i <> 1 Then Set wks = xl.Worksheets.Add 'Add a Wooksheet to the Excel File End If wbk.Sheets("Sheet" & i).Select 'Select the new Sheet wbk.Sheets("Sheet" & i).Name = rstList("SheetName") 'Rename the Sheet Set wks = wbk.activesheet 'Obtain and Write Data to the Excel Sheet strSQLRpt = "Select * from [" & rstList("QueryName") & "]" Set objMyRange = wks.Cells(xl.activesheet.UsedRange.Rows.Count + 1, 1) Set rstRpt = dbs.OpenRecordset(strSQLRpt, dbOpenSnapshot, dbReadOnly) If rstRpt.RecordCount = 0 Then GoTo Exit_GenerateQualityReportsSub Else rstRpt.MoveLast DiscrepancyRecords = rstRpt.RecordCount rstRpt.MoveFirst End If 'Write the Column Headers to the Sheet For col = 0 To rstRpt.Fields.Count - 1 wks.Cells(1, col + 1) = rstRpt.Fields(col).Name Next col 'Write Data to the Excel Sheeet Range("A2").Select With objMyRange rstRpt.MoveFirst .CopyFromRecordset rstRpt End With 'Format the Sheet Cells Cells.Select Selection.Columns.AutoFit Range("A1").Select 'Save the Excel File wbk.Save 'Save the Excel File NextReport: 'Close the Data Results rstRpt.Close Set rstRpt = Nothing rstList.MoveNext i = i + 1 Wend i = i - 1 'Close the Excel File and Application xl.Visible = True wbk.Save wbk.Close savechanges:=True xl.Quit Set wks = Nothing DoEvents Set wbk = Nothing DoEvents Set xl = Nothing DoEvents NeedToCloseExcel = False 'Close the Report Record rstList.Close Set rstList = Nothing Exit_GenerateQualityReportsSub: If NeedToCloseExcel Then xl.Visible = True wbk.Save wbk.Close savechanges:=True xl.Quit Set wks = Nothing DoEvents Set wbk = Nothing DoEvents Set xl = Nothing DoEvents NeedToCloseExcel = False End If Exit Sub ERR_GenerateQualityReportsSub: ..... End Sub 

我会推荐:

不要创build一个新的Excel应用程序,首先尝试重新使用现有的应用程序。 确保在退出Excel应用程序之前将用于操作Excel对象的variables设置为Nothing 。 在你的代码中,你退出应用程序,但仍然保持对一些variables的引用。

 '----------------------------------------------------------------------------- ' Return an intance of Excel ' First tries to open an existing instance. If it fails, it will create an instance. ' If that fails too, then we return 'Nothing' '----------------------------------------------------------------------------- Public Function GetExcelObject() As Object On Error Resume Next Dim xlo As Object ' Try to get running instance of Excel Set xlo = GetObject("Excel.Application") If xlo Is Nothing Then Set xlo = CreateObject("Excel.Application") End If Set GetExcelObject = xlo End Function 

然后使用:

 Set xl = GetExcelObject() 

当你完成你的Excel文件时:

 ' Clear all variables that were used to contain Excel objects set objMyRange = nothing set Range = nothing set Selection = nothing ' Save and close wbk.Save wbk.Close savechanges:=True Set wks = Nothing Set wbk = Nothing xl.Quit Set xl = Nothing 

我想你需要修改一下你的代码。 也许你没有在这里包括所有的东西,但有一些可疑的东西:

  • 你使用未声明的variables(select,单元格,范围)。 也许你没有包含声明它们的代码,但是确保你在所有的VBA文件的顶部都有Option Explicit来强制你声明所有的variables。

  • 你有一些GoTo Exit_GenerateQualityReportsSub这通常是你的代码需要重构的标志。 除了VBA中的错误pipe理之外,您还需要使用GoTo 。 在这种情况下,您可以完美地使用Exit Do退出循环。
    这些跳跃使得代码变得更难推理。

  • 你也在你的函数的末尾重复了相同的清理代码来处理GoTo分支。 通过复制你的代码,你冒着使它不易维护的风险,你可能会因为忘记更新这两个块而引入错误。