Perhaps a bit of brute force -
Sub test3()
Dim bRes As Boolean
Dim nFirstDriveToSearch As Long
Dim sKnownFile As String
Dim sDrive As String
nFirstDriveToSearch = Asc("E") ' or simply 69 (maybe start with C)
' a known file that only exists in the drive to be found
sKnownFile = "aler.ini" ' this in root but could add folder
bRes = GetMyDrive(nFirstDriveToSearch, sKnownFile, sDrive)
If bRes Then
MsgBox sDrive
Else
MsgBox "not found"
End If
End Sub
Function GetMyDrive(nChr As Long, sFile As String, sDrive As String) As
Boolean
Dim bRes As Boolean
On Error Resume Next
For i = nChr To Asc("Z")
sDrive = Chr(i) & ":\"
bRes = False
bRes = GetAttr(sDrive) = vbDirectory
If bRes Then
bRes = fbFileExists(sDrive & sFile)
If bRes Then
Exit For
End If
End If
Next
GetMyDrive = bRes
End Function
Function fbFileExists(ByVal sFile As String) As Boolean
Dim nAttr As Long
On Error Resume Next
nAttr = GetAttr(sFile)
fbFileExists = (Err.Number = 0) And ((nAttr And vbDirectory) = 0)
On Error GoTo 0
End Function
Regards,
Peter T
"zhj23" <(E-Mail Removed)> wrote in message
news:9B88722B-D2D7-468B-8E8D-(E-Mail Removed)...
> For mobility reasons, I store all my VBA programmes in a thumb drive
(USB).
> And in some of my VBA programmes, also contain the drive path with the
> associated drive letters. (e.g. opening a file)
>
> As I add more USB devices to my PC, these drive letters keep changing such
> that the VBA programme can't read the correct drive letter where the files
> are stored.
>
> Any solution to fix the drive letter? Thanks.
>
> zhj23
|