【问题标题】:Excel process stuck in Task ManagerExcel 进程卡在任务管理器中
【发布时间】:2021-01-02 23:35:41
【问题描述】:

在 Access 中,我打开一个 Excel 文件,从中读取并关闭它。 Excel 进程不会离开任务管理器。

我发现了同样的问题here,但没有有效的解决方案。

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

我有以下课程,称为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 的进程仍然存在于任务管理器进程选项卡中。

【问题讨论】:

  • 如果你将Debug.Print "finshed" 语句放在析构函数的最结束,它会被执行吗?
  • 如果您从代码中完全删除 on error - 有什么变化吗?我怀疑它与 VBA 实现类的方式有关,或者您的应用程序中有一些其他代码没有释放对象。如果到今天下午还没有答案,我会尝试重新制作。如果我不能重新制作,那么它一定是你的代码中的其他内容没有发布参考
  • 是的,如果我按照要求将 debug.print “完成”放置,它就会被执行。它将在即时窗口中显示为最后打印的行。
  • 我从模块代码中注释掉了所有对错误和try-catch-finally标签的引用,现在它只有四行代码(dim... set ... print .. set.. ..),问题依然存在。
  • 我一直试图弄清楚发生了什么。现在我发现,如果我从类中注释掉(删除) iLastRow = oWS.Range("A" & Rows.Count).End(xlUp).row 行,问题就会消失。但这意味着我不能对班级内的 Excel 文件做任何事情......没用的班级。有什么帮助吗?

标签: excel vba ms-access


【解决方案1】:

很好的故障排除!

尝试从

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

对此(您可能需要更进一步并在应用程序级别引用范围,我不记得了,现在无法测试

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

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

【讨论】:

  • 你成功了!问题解决了。谢谢!没想到,这个问题快把我逼疯了。
【解决方案2】:

在我的场景中,excel 和其他办公应用程序没有从 TaskManager 完全关闭,因为我正在运行 Fiddler 工具(用于跟踪 http 请求,因为我主要在 Web 应用程序上工作)。

如果我关闭或停止在提琴手工具上捕获,那么它会正确地从任务管理器中退出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多