gpt4 book ai didi

Excel 进程卡在任务管理器中

转载 作者:行者123 更新时间:2023-12-02 17:42:33 26 4
gpt4 key购买 nike

在 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 进程仍然存在于“任务管理器进程”选项卡中。

最佳答案

很好的故障排除!

尝试更改有问题的行

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

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

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

可能的问题是您没有完全合格的引用资料,请参阅 here

关于Excel 进程卡在任务管理器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42194543/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com