Tuesday, November 12, 2013

Map & Unmap drives via VBA

VBA Code to map and unmap a drive in the system:

Sub Map_Drive()
Dim oNetwork As Object, sDrive As String, sPath As String

Set oNetwork = CreateObject("WScript.Network")
sDrive = "A:"
sPath = "\\Server1\ABC"
oNetwork.MapNetworkDrive sDrive, sPath

sDrive = "B:"
sPath = "\\Server2\XYZ"
oNetwork.MapNetworkDrive sDrive, sPath

End Sub
----------------------------------------------------------
Sub Unmap_Drive()
Dim objNetwork As Object

Set objNetwork = CreateObject("WScript.Network")

objNetwork.RemoveNetworkDrive "A:" ', bForce:=True
objNetwork.RemoveNetworkDrive "B:" ', bForce:=True
End Sub

1 comment:

  1. VBA Function for RemoveNetworkDrive is as follows:
    object.RemoveNetworkDrive(strName, [bForce], [bUpdateProfile])

    Example of using this function would be:
    oNetwork.RemoveNetworkDrive ENTERDRIVELETTER ,true ,true

    ReplyDelete