About This Code
Brief Description:
tests for network drive mapping and maps drives if needed
Contributor:
Joseph LeMay
Category:
Lotusscript, VB
Notes Version:
R6.x, R8.x, R7.x
Last Modified:
30 Jul 2009
OpenNTF Disclaimer
All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.
Code / Description
Option Public
Option Declare
Dim mappedDrives As Boolean
Sub Initialize
Call mapNetworkDrives
If Not mappedDrives Then Exit Sub
End Sub
Sub mapNetworkDrives
Dim network As Variant
Set network = createobject("WScript.Network")
Dim drivesAreGood As Boolean
drivesAreGood = checkMapping("h:", "\\emrml01\projects") 'substitute your own drive letter and UNC path here.
If drivesAreGood Then
Messagebox "Drive H is mapped to \\emrml01\Projects",0,"Drive mapped correctly"
mappedDrives = True
Else
Messagebox "Drive H is not mapped to \\emrml01\Projects. I will now attempt to map it myself.",0,"Drive not mapped correctly"
Call network.MapNetworkDrive("H:", "\\emrml01\Projects")
'see if we were able to connect
If checkMapping("h:", "\\emrml01\projects") Then
Messagebox "I did it!! Drive H is mapped to \\emrml01\Projects",0,"Drive mapped correctly"
mappedDrives = True
Else
Messagebox "Can't map the drive. Your H drive must be mapped to \\emrml01\Projects to proceed. Please connect to the network.",0,"Stopping"
End If
End If
End Sub
Function checkMapping(driveLetterCheck As String, uncCheck As String) As Boolean
'given drive letter and unc path, check to make sure it's mapped OK.
Dim i As Integer
Dim drives As Variant
Dim net As Variant
Dim networkResult As Variant
Dim driveLetter As String
Dim unc As String
Dim drivesAreGood As Boolean
Set net = createobject("WScript.Network")
Set drives = net.EnumNetworkDrives()
checkMapping = False
For i = 0 To drives.Count() - 1 Step 2
driveLetter = Lcase(drives.Item(i))
unc = Lcase(drives.Item(i+1))
If driveLetter = driveLetterCheck And unc = uncCheck Then
checkMapping = True
Exit For
End If
Next
End Function
Usage / Example
Works on Win32 platforms. Substitute your own drive letter and UNC path name, it will check if that drive letter is mapped to that UNC path. If not, it will attempt to map the drive on it's own.