使用VBA从Access运行后,从任务pipe理器中删除Excel任务

我现在趁机问这里,我真的尝试了很多不同的方式,但似乎我无法closures任务pipe理器中的Excel任务,直到我完全closuresAccess,烦人,因为我无法使用Access从Excel运行两个不同的作业。 第二份工作会给我错误。

我已经提出了一些意见,我仍然可以摆脱Excel。 代码的目的是运行一些查询和导出数据到excel,然后lockingexcel表格,这样用户只能填写数据的答案。

码:

Private Sub Command65_Click() Dim r As Double 'On Error GoTo Error_Handler Dim objExcel As Excel.Application Dim objWorkbook As Workbook Dim objWorksheet As Worksheet Dim dbs As DAO.Database Dim rSt As DAO.Recordset Set dbs = CurrentDb Set rSt = CurrentDb.OpenRecordset("qry_VC_Confirmation") Set objExcel = CreateObject("Excel.Application") objExcel.Visible = True 'objExcel.Quit ' at this point it still works to close again 'Set objExcel = Nothing ' at this point it will remove from task manager Set objWorkbook = objExcel.Workbooks.Add Set objWorksheet = objWorkbook.Worksheets(1) 'Set objWorkbook = Nothing ' can close still at this stage 'Set objWorksheet = Nothing ' can close still at this stage 'objExcel.Quit ' at this point it still works to close again ? 'Set objExcel = Nothing ' at this point it still will not remove from task manager iFld = 0 irow = 1 For icol = 1 To (rSt.Fields.count) objWorksheet.Cells(irow, icol) = rSt.Fields(iFld).Name objWorksheet.Cells(irow, icol).Interior.ColorIndex = 1 objWorksheet.Cells(irow, icol).Font.ColorIndex = 2 objWorksheet.Cells(irow, icol).Font.Bold = True iFld = iFld + 1 Next 'Set objWorkbook = Nothing ' 'Set objWorksheet = Nothing ' 'objExcel.Quit ' at this point it still works to close Excel again ? 'Set objExcel = Nothing ' at this point it will still remove from task manager irow = 2 If Not rSt.BOF Then rSt.MoveFirst Do Until rSt.EOF iFld = 0 lRecords = lRecords + 1 For icol = 1 To (rSt.Fields.count) objWorksheet.Cells(irow, icol) = rSt.Fields(iFld) iFld = iFld + 1 Next irow = irow + 1 rSt.MoveNext Loop r = irow - 1 Columns("A:F").EntireColumn.AutoFit ActiveSheet.Protection.AllowEditRanges.Add Title:="Unprotected", Range:=Range("F2:F" & r) ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:="secret" objWorkbook.SaveAs ("C:\Dropbox\VC_Confirmation.xlsx") ExitSub: Set objWorkbook = Nothing ' Set objWorksheet = Nothing ' objExcel.Quit ' at this point it still works to close excel again ? Set objExcel = Nothing ' at this point it will **NOT** remove from task manager Exit Sub Error_Handler: MsgBox Error$ Resume ExitSub End Sub 

在你提到的评论中,你已经重置你的代码(即按下停止button)。 这意味着你杀死excel的代码部分没有运行,因此留下了一个开放的excel会话。 你的代码有一个小的(可能是语义上的)问题,但是我不相信那是什么导致你的问题。 无论如何,你应该像这样正确地closures应用程序。

 ExitSub: If Not objWorksheet Is Nothing Then set objWorksheet = Nothing End If ' You have to check for the workbook's existence before ' you try to close something that isn't there. This avoids runtime errors. ' Since your error handler points you back here, this code always runs, so ' The workbook might not be open. If Not objWorkbook Is Nothing Then objWorkbook.close Set objWorkbook = Nothing End If ' Same goes for quitting the application If Not objExcel Is Nothing Then objExcel.Quit Set objExcel = Nothing End If Exit Sub Error_Handler: ' error handling code here Resume ExitSub End Sub 
 Columns("A:F").EntireColumn.AutoFit 

以防万一。 完全符合工作表名称,然后重试。 同样的问题对我也是一个很大的麻烦。 无论如何,你必须有100%的参考资格。 另外,在范围,工作表等上使用With语句时要特别小心,因此将其更改为ObjWorksheet.Columns(“A:F”)…而是