Hello ImadEdd,
This macro will return the UNC for a mapped drive. You can use this to return the UNC for the drive on your computer. You can then create a loop in your macro to test if any mapped drives on the user's computer match. Both macros can be copied to the same module.
Macro to return UNC Path of a Mapped Drive
Option Explicit
' Written: October 02, 2018
' Author: Leith Ross
' Summary: Returns the path of a mapped drive using the drive letter.
Private Declare PtrSafe Function WNetGetConnection Lib "Mpr.dll" Alias "WNetGetConnectionA" (ByVal lpLocalName As String, ByVal lpRemoteName As String, ByRef lpnLength) As Long
Private Declare PtrSafe Function StrLen Lib "kernel32.dll" Alias "lstrlenA" (ByVal lpString As String) As Long
Function GetMappedPath(ByVal DriveLetter As String) As String
Dim Buffer As String
Dim BuffLen As Long
Dim ret As Long
BuffLen = 260
Buffer = String(BuffLen, Chr(0))
DriveLetter = IIf(Right(DriveLetter, 1) <> ":", DriveLetter & ":", DriveLetter)
ret = WNetGetConnection(DriveLetter, Buffer, BuffLen)
GetMappedPath = Left(Buffer, StrLen(Buffer))
End Function
Loop to check User's Computer for Same Path
Sub PathTest()
Dim Id As Long
Dim Match As String
Dim MyPath As String
Dim Path As String
' // Hard coded path for drive Z on my computer.
MyPath = "\\Epsona8dfdc\memorycard"
' // Check letters E through Z for a match.
For Id = 69 To 90
Path = GetMappedPath(Chr(Id))
If Path = MyPath Then
Match = Path
End If
Next Id
' // Exit if no match was found.
If Match = "" Then
MsgBox "No Matching Mapped Drive was Found.", vbCritical
Exit Sub
End If
End Sub
Bookmarks