MS Access 2010 VBA Excel进程未终止

时间:2015-02-17 06:59:20

标签: excel vba process ms-access-2010 orphan

我正在使用Window 7 SP1和MS Office 2010.我在Access 2010中运行查询,并将结果写入Excel 2010的报告中,并注意到Excel的实例不会从进程列表中终止。为了确保它不是我的代码,我已经删除了基础知识。

  1. 以下代码按预期打开并终止Excel:

    Public Sub Test()
        Dim strRptDirPath As String: strRptDirPath = "C:\Projects\WeeklyAlarms\Report\"
        Dim xlApp As Object
     '   Dim xlWkBk As Object
     '   Dim xlWkSht As Object
        Dim strRptTl As String: strRptTl = "Report Template.xls"
        Dim strRptSht As String: strRptSht = "Rpt"
    
        Set xlApp = CreateObject("Excel.Application")
            xlApp.Visible = False
    
    '    Set xlWkBk = xlApp.Workbooks.Open(strRptDir & strRptTl)
    '    Set xlWkSht = xlWkBk.Worksheets(strRptSht)
    '
    '        xlWkSht.Cells(1, 1).Value = "TEST 1"
    '        xlWkSht.Cells(2, 2).Value = "TEST 2"
    '        xlWkSht.Cells(3, 3).Value = "TEST 3"
    '
    '        xlWkBk.SaveAs (strRptDirPath & "TESTING.xls")
    '        xlWkBk.Close
            xlApp.Quit
    
    '    Set xlWkSht = Nothing
    '    Set xlWkBk = Nothing
        Set xlApp = Nothing
    
    End Sub
    
  2. 但是,如果我引用工作簿,则Excel流程实例不会终止。通过谈话主题进行搜索,可以在访问关闭后超时“ping”一段时间后终止对该过程的引用 - 这不是这种情况。

    Public Sub Test()
        Dim strRptDirPath As String: strRptDirPath = "C:\Projects\WeeklyAlarms\Report\"
        Dim xlApp As Object
        Dim xlWkBk As Object
    '   Dim xlWkSht As Object
        Dim strRptTl As String: strRptTl = "Report Template.xls"
        Dim strRptSht As String: strRptSht = "Rpt"
    
        Set xlApp = CreateObject("Excel.Application")
            xlApp.Visible = False
    
        Set xlWkBk = xlApp.Workbooks.Open(strRptDir & strRptTl)
    '    Set xlWkSht = xlWkBk.Worksheets(strRptSht)
    '
    '        xlWkSht.Cells(1, 1).Value = "TEST 1"
    '        xlWkSht.Cells(2, 2).Value = "TEST 2"
    '        xlWkSht.Cells(3, 3).Value = "TEST 3"
    
            xlWkBk.SaveAs (strRptDirPath & "TESTING.xls")
            xlWkBk.Close
            xlApp.Quit
    
    '   Set xlWkSht = Nothing
       Set xlWkBk = Nothing
       Set xlApp = Nothing
    
    End Sub
    
  3. 我已取消选中Microsoft Excel 14.0对象库参考。我认为我没有做过任何全球参考电话。我看不出我做错了什么。

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码替换xlWkBk.SaveAs (strRptDirPath & "TESTING.xls")行之后的所有内容:

If Not xlWkBk Is Nothing Then
    xlWkBk.Save
    xlWkBk.Close
    Set xlWkBk = Nothing
End If
If Not xlApp Is Nothing Then
    xlApp.Quit
    Set xlApp = Nothing
End If

这样设置确保一切按预期结束