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

No comments:

Post a Comment