使用 VBA 从 Access 运行后从任务管理器中删除 Excel 任务
Posted
技术标签:
【中文标题】使用 VBA 从 Access 运行后从任务管理器中删除 Excel 任务【英文标题】:Remove Excel task from Task manager after running from Access using VBA 【发布时间】:2014-03-19 16:54:43 【问题描述】:我现在借机在这里问一下,我确实尝试了很多不同的方法,但是我似乎无法关闭任务管理器中的Excel任务,直到我关闭Access才会挂起完全,烦人,因为我无法使用 Access 中的 Excel 运行两个不同的作业。第二份工作会给我错误。
我已经做了一些 cmets,我仍然可以摆脱 Excel。 该代码的目的是运行一些查询并将数据导出到excel,然后锁定excel表,以便用户只能填写数据的答案。
代码:
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
【问题讨论】:
也许你把objWorkbook.Close False
放在ExitSub:
之后
> 关闭工作簿,然后退出 Excel,然后将对象设置为 Nothing
谢谢!我试过 objWorkbook.Close False 没有成功,
>>艾伦,如果你的意思是 objExcel.quit 我已经试过了。在大多数情况下,我得到“对象变量或未设置块变量”,奇怪的是它以某种方式插入所有值的最后一个 For 循环导致这一切发生。
请在您的评论所针对的用户名前添加一个“@”,以便他或她收到通知。您可以在评论时单击帮助以查看更多信息。
【参考方案1】:
在您提到的 cmets 中,您已重置代码(即按下停止按钮)。这意味着杀死 excel 的代码部分没有运行,因此留下了一个开放的 excel 会话。您的代码存在一个小问题(可能是语义问题),但我认为这不是导致您问题的原因。无论如何,您应该像这样正确关闭应用程序。
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
【讨论】:
非常感谢您添加此建议,我会尽快尝试。【参考方案2】:Columns("A:F").EntireColumn.AutoFit
添加作为答案以防万一。使用工作表名称完全限定它,然后重试。同样的问题对我来说也是一个巨大的困扰。无论如何,您必须 100% 符合您的推荐信。另外,在范围、工作表等上使用 With 语句时要格外小心。所以将其更改为 ObjWorksheet.Columns("A:F")...
【讨论】:
以上是关于使用 VBA 从 Access 运行后从任务管理器中删除 Excel 任务的主要内容,如果未能解决你的问题,请参考以下文章