【发布时间】: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 循环导致这一切发生。
-
请在您的评论所针对的用户名前添加一个“@”,以便他或她收到通知。您可以在评论时单击帮助以查看更多信息。