OVH Cloud OVH Cloud

Connecter un lecteur réseau!

1 réponse
Avatar
Kyvu
Bonjour =E0 tous,

Est-il possible de connecter un lecteur en VBA?

Merci!

@micalement.



Kyvu On Line!

1 réponse

Avatar
Michel Pierron
Bonjour k.tran;
Un exemple :
Private Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias _
"WNetCancelConnection2A" (ByVal lpName As String _
, ByVal dwFlags As Long _
, ByVal fForce As Long) As Long
Private Declare Function WNetAddConnection2 Lib "mpr.dll" Alias _
"WNetAddConnection2A" (lpNetResource As NETRESOURCE _
, ByVal lpPassword As String _
, ByVal lpUserName As String _
, ByVal dwFlags As Long) As Long

Private Type NETRESOURCE
dwScope As Long
dwType As Long
dwDisplayType As Long
dwUsage As Long
lpLocalName As String
lpRemoteName As String
lpComment As String
lpProvider As String
End Type

Private Const wDisk As String = "R:"
Private Const PathName As String = "ServerProductionsGpao"

Sub Test_Deconnexion()
Const NO_ERROR = 0
Const ERROR_NOT_CONNECTED = 2250&
Const CONNECT_UPDATE_PROFILE = &H1
Dim dwResult As Long

' Abandon si fichiers ouverts ou en cours d'utilisation
dwResult = WNetCancelConnection2(wDisk, CONNECT_UPDATE_PROFILE, False)
If dwResult = ERROR_NOT_CONNECTED Then
MsgBox "Lecteur " & wDisk & " non connecté !", 64
Else
If dwResult = NO_ERROR Then MsgBox "Lecteur " & wDisk & " déconnecté !", 64
End If
End Sub

Sub Test_Connexion()
Const ERROR_DEVICE_ALREADY_REMEMBERED = 1202&
Const ERROR_ALREADY_ASSIGNED = 85&
Dim dwResult As Long, NR As NETRESOURCE

NR.dwType = 1: NR.lpRemoteName = Trim(PathName): NR.lpLocalName = wDisk
dwResult = WNetAddConnection2(NR, vbNullString, vbNullString, 1)
If dwResult = ERROR_ALREADY_ASSIGNED Then
MsgBox "Ce lecteur est déjà connecté à cette ressource réseau !", 48
ElseIf dwResult = ERROR_DEVICE_ALREADY_REMEMBERED Then
MsgBox "Lecteur déjà affecté dans le profil d'utilisateur !", 48
Else
MsgBox "Lecteur " & wDisk & " connecté !", 64
End If
End Sub

MP

"Kyvu" a écrit dans le message de
news:214f01c38359$f2794e30$
Bonjour à tous,

Est-il possible de connecter un lecteur en VBA?

Merci!

@micalement.



Kyvu On Line!