Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel process stuck in Task Manager

In Access I open an Excel file, read from it and close it. The Excel process does not go away from Task Manager.

I found the same problem here but no solution that worked.

If I click the Reset button in the VB Editor, it goes away (or if I change anything in the code, which also causes the project to reset and gets rid of the unwanted Excel process).

I have the following class, called 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

I have the following Module to use above class:

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

I get the result I want:

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

There are no errors but a process for Excel persists in Task Manager Processes tab.

like image 298
John Harvard Avatar asked Mar 04 '26 07:03

John Harvard


1 Answers

nice troubleshooting!

try changing the problematic line from

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

to this (you may need to go further and reference the range at the Application level, I can't remember this and can't test now)

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

The likely issue is that you do not have a fully qualified reference, see here

like image 108
brettdj Avatar answered Mar 06 '26 23:03

brettdj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!