(Solved) – Folder handle not released after Dir checks for existence

  • by
(solved)-–-folder-handle-not-released-after-dir-checks-for-existence

The following code checks to see if a folder exists and, if not, creates it. The code works, but a handle that points to the folder is left open if it already exists, which prevents the folder from being deleted or renamed until Outlook.exe closes. I do not understand why this is happening or what to do about it, but a handle should not be open after the folder is checked and potentially created.

Sub Test()
    Folder = Environ("USERPROFILE") & "DesktopNewFolder"
    Result = Dir(Folder, vbDirectory)
    If Result = vbNullString Then
        MkDir Folder
    End If
End Sub

The first time through the code, the folder is successfully created and no file handles are open:

enter image description here

However, the second time through the code, the folder already exists. MkDir does not execute, but a file handle is left open presumably after Dir executes:

enter image description here

I have tried everything I could find to close all open file handles, but nothing has worked. Any help would be greatly appreciated.

Leave a Reply

Your email address will not be published. Required fields are marked *