I'm writing a script to open documents using the default program via the Windows shell based on this SO answer:
Dim Shex As Object
Set Shex = CreateObject("Shell.Application")
tgtfile = "C:\Nax\dud.txt"
Shex.Open (tgtfile)
I notice the instance of Shell.Application
never gets closed. In my code, I Set Shex = Nothing
, but is that enough? If I create a Word or Outlook instance, for example, I would need to close it with .Quit
before setting the variable to nothing. There's nothing obviously analogous going on here.
I set a reference to Microsoft Shell Controls and Automation
to explore the Shell
object, but couldn't find any methods for the .Application
or .Parent
properties, let alone one that looked like .Quit
.
Am I missing something obvious? Does the garbage collector somehow also get rid of the instance? Is it something specific to the shell object itself?
Thinking about it, I'm pretty sure @jamheadart is right and I'm just instancing a VBA class rather than creating an application class in Windows.
To be sure, though, I'm taking @Mert Y's suggestion of using a context manager to limit scope.
Final code:
With CreateObject("Shell.Application")
.Open (strPath)
End With