使用VBA从Access运行后删除从任务管理器的Excel任务使用VBA从Access运行后删除从任

2019-05-12 09:44发布

我现在就趁机问在这里,我真的尝试了很多不同的方式,但似乎我无法能够关闭在任务管理器的Excel的任务,它挂起,直到我关闭Access完全,烦人,因为我不能运行使用Excel从Access的两个不同的工作。 第二工作可以给我的错误。

我已经在那里我仍然能够摆脱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

Answer 1:

在评论你提到你不得不重置您的代码(即按下停止按钮)。 这意味着,杀死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


Answer 2:

Columns("A:F").EntireColumn.AutoFit

添加为以防万一答案。 与工作表的名称完全限定并再次尝试。 同样的问题是一个巨大的麻烦对我来说太。 你必须符合你的推荐100%,不管是什么。 而且,是超级小心使用随着范围的陈述,工作表等,所以将其更改为ObjWorksheet.Columns(“A:F”)......而不是



文章来源: Remove Excel task from Task manager after running from Access using VBA