How to free an object and clear memory in VBA

This is the first time I am posting a plus to them as an amateur programmer, so let me know if you need more information. I have the following problem:

with excel VBA, I am connecting to another program (namely Aspen EDR). For this I have the corresponding add-on installed. to access Aspen EDR I need to add an object. after im done i want to free the object to save some memory. The first thing I tried was:

Dim ObjEDR      As BJACApp
Dim Path        As String

Path = 'assume this is the correct path to the file i want to open
Set ObjEDR = New BJACApp ' Create the BJAC object

If Not ObjEDR.FileOpen(Path) Then
    MsgBox "Can't open file!"
End If

'...

Set ObjEDR = Nothing

      

My problem: after I have not installed anything, excel does not release memory (as I can see in my task manager). of course, after a few hundred iterations (I have to open a lot of these files), I get an error that excel is out of memory. i read several threads and apparently nothing deletes only some object reference but not the object itself, so i tried adding fileclose

'...

ObjEDR.FileClose
Set ObjEDR = Nothing

      

when doing FileClose I see a small amount of memory released (0.5 out of 3 MB), but a lot of memory has accumulated.

Thanks for the help:)

+3


source to share


1 answer


Remove the keyword New

, it is not necessary.



Dim ObjEDR      As BJACApp
Dim Path        As String

Path = 'assume this is the correct path to the file i want to open
Set ObjEDR = BJACApp ' Create the BJAC object

If Not ObjEDR.FileOpen(Path) Then
    MsgBox "Can't open file!"
End If

'...

ObjEDR.FileClose
Set ObjEDR = Nothing

      

+1


source







All Articles