代码清理后,Excel进程卡在任务管理器中

时间:2017-02-12 23:33:27

标签: excel vba access

我在访问打开Excel文件,从中读取并关闭它时遇到问题

...但Excel进程不会离开任务管理器。

我找到了完全相同的问题here,但没有解决方案对我有用,所以我在这里发布我的情况(见下文)。

如果我单击VB编辑器中的“重置”按钮,它就会消失(或者如果我更改代码中的任何内容,这也会导致项目重置并摆脱不需要的Excel过程。)

有关代码有什么问题的想法(在Access中运行)?

代码

我有以下课程,名为clsTest

Option Compare Database
Option Explicit

Private xlFolder As String
Private xlFile As String
Private xlSheet As String
Private colShortURL As String
Private oXL As Excel.Application
Private oWB As Excel.Workbook
Private oWS As Excel.Worksheet
Private iLastRow As Long

Private Sub Class_Initialize()
    Debug.Print "From class: Going through initialization inside class - constructor"
    xlFolder = "E:\COMH\Excel"
    xlFile = "Records v8z.xlsm"
    xlSheet = "comh"
    Set oXL = New Excel.Application
    Set oWB = oXL.Workbooks.Open(Filename:=(xlFolder & "\" & xlFile), ReadOnly:=True)
    Set oWS = oWB.Sheets(xlSheet)
    iLastRow = oWS.Range("A" & Rows.Count).End(xlUp).row
End Sub

Public Property Get ShortURL() As String
    ShortURL = "Hello World " & iLastRow
End Property

Private Sub Class_Terminate()
    Debug.Print "From class: Going through the clean-up inside class - destructor"
    oWB.Close SaveChanges:=False
    Set oWS = Nothing
    Set oWB = Nothing
    oXL.Quit
    Set oXL = Nothing
End Sub

我在上面的课程中使用了以下模块:

Option Compare Database
Option Explicit

Private Sub TestClass()
    Dim newExcel As clsTest
Try:
    On Error GoTo Catch
    Set newExcel = New clsTest
    Debug.Print "Class instantiated, all good"
    Debug.Print "ShortURL=" & newExcel.ShortURL
    GoTo Finally
Catch:
    Debug.Print "dealing with the error"
    Debug.Print Err.Description & " - " & Err.Number
Finally:
    Debug.Print "doing the finally stuff"
    Set newExcel = Nothing
End Sub

当我运行上面的代码时,一切正常,我得到了我想要的结果:

From class: Going through initialization inside class - constructor
Class instantiated, all good
ShortURL=Hello World 2603
doing the finally stuff
From class: Going through the clean-up inside class - destructor

没有错误,但Excel的进程仍然存在于“任务管理器进程”选项卡中。

2 个答案:

答案 0 :(得分:3)

很好的故障排除!

尝试从

更改有问题的行
 iLastRow = oWS.Range("A" & Rows.Count).End(xlUp).row

到此(您可能需要更进一步并在应用程序级别引用范围,我无法记住这一点,现在无法测试

iLastRow = oWS.Range("A" & oWS.Rows.Count).End(xlUp).row

可能的问题是您没有完全限定的参考,请参阅here

答案 1 :(得分:0)

在我的情况下,Excel和其他Office应用程序未从TaskManager完全关闭,因为我正在运行 Fiddler 工具(用于跟踪http请求,因为我主要在Web应用程序上工作)。

如果我关闭或停止在提琴手工具上捕获,则它会正确退出TaskManager。