View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
[email protected] horkuang@horkuang.com is offline
external usenet poster
 
Posts: 8
Default How to check drive exist or not using FileSystemObject

Just use VBA and error trapping
loDriver = Left(txtfilename.Value, 1)

On Error Resume Next
ChDrive loDriver

If Err.Number 0 Then
MsgBox "Drive """ & loDriver & """ not available"
Exit Sub
Else
On Error GoTo 0
'Continue
End If

NickHK

moonhk wrote:
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