'Vba, "This network connection has files open or requests pending"

If I run the following, it works and removes the network drive,

Set objNet = CreateObject("WScript.Network")
objNet.RemoveNetworkDrive "A:"

However if I run this,

If Len(Dir("A:\", vbDirectory)) > 0 Then
    Set objNet = CreateObject("WScript.Network")
    objNet.RemoveNetworkDrive "A:"
End If

I get the following error,

"This network connection has files open or requests pending"
vba


Solution 1:[1]

Don't use Dir, try this:

Set fso = CreateObject("scripting.filesystemobject")
If fso.DriveExists("A") Then
    Set objNet = CreateObject("WScript.Network")
    objNet.RemoveNetworkDrive "A:"
End If

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rosetta