Je l'ai fait avec le Scripting.FileSystemObject
:
Public Function MapDrive(ByVal Sharename As String, DriveToMap As String) As Boolean
On Error GoTo Handler
Dim fso As Scripting.FileSystemObject
Dim ntwk As IWshRuntimeLibrary.IWshNetwork_Class
' Assume success; any failure will invoke the error handler & cause '
' the function to return false. '
MapDrive = True
Set fso = New Scripting.FileSystemObject
Set ntwk = New IWshRuntimeLibrary.IWshNetwork_Class
' If the specified drive doesn't even exist, just map it '
If Not fso.DriveExists(DriveToMap) Then
ntwk.MapNetworkDrive DriveToMap, Sharename
Exit Function
End If
' The drive already exists; see if it's already be mapped correctly. '
If UCase(fso.Drives(DriveToMap).ShareName) = UCase(Sharename) Then
Exit Function
End If
' The drive is mapped, but to the wrong place. Unmap, then map the drive. '
ntwk.RemoveNetworkDrive DriveToMap
ntwk.MapNetworkDrive DriveToMap, Sharename
Exit Function
Handler:
MapDrive = False
Err.Clear
End Function