View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Chip Pearson Chip Pearson is offline
external usenet poster
 
Posts: 7,247
Default How to check drive exist or not using FileSystemObject

You could do with a a variety of API calls, but the simplest way is to try
to ChDrive to the drive in question and test for an error:

Dim SaveDir As String
SaveDir = CurDir ' save CurDir to restore defaults
On Error Resume Next
ChDrive "Z:\" ' test for drive 'Z'
If Err.Number < 0 Then
Debug.Print "Drive does not exist"
Else
Debug.Print "Drive exists."
End If
ChDrive SaveDir
ChDir SaveDir



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)

"moonhk" wrote in message
ups.com...
Dear Reader


How to check drive exist or not using FileSystemObject ?

Private Sub cmdSelectName_Click()
Dim myFileName As Variant
Dim FileObj As New FileSystemObject
Dim loDriver As String
Dim loFolder As String
Dim loFilename As String
On Error Resume Next
loDriver = VBA.Trim(VBA.Left(txtfilename.Value, 2))
'~~ Check Drive exist of not

'~~ Change Drive
VBA.ChDrive (loDriver)
loFilename = FileObj.GetFileName(txtfilename.Value) '~~ Return
file name
If VBA.InStr(1, txtfilename.Value, loFilename, vbTextCompare) 0
Then
loFolder = VBA.Left(txtfilename, VBA.InStr(1,
txtfilename.Value, loFilename, vbTextCompare) - 2)
End If

If FileObj.FolderExists(loFolder) Then
VBA.ChDir (loFolder)
End If
On Error GoTo 0
myFileName = Application.GetOpenFilename(filefilter:="Prn Files,
*.PRN", _
Title:="Pick a File")

If myFileName = False Then
'~~ MsgBox "Ok, try later" '~~user select cancel
Exit Sub
Else
txtfilename.Value = myFileName
End If
End Sub