Reputation: 47
Run-time error '52': Bad file name or number
I would like to ask for your help and suggestions as to why my code encounters a "run-time error '52': bad file name or number" when I am using a computer which do not really have access to the directory drive. I tried it on my personal computer and it showed the run-time error.
My code is working fine when I am using our company computers which have access to the directory drive. And it displays the message box "unable to access" if I try to change the folder name to make it inaccessible (for troubleshooting purposes).
What I am trying to do is actually display a message box just in case the computer used do not have any access to the directory.
I tried searching around, tried using "", 0, and vbNullString. But no success. Thank you in advance for any help.
'Check if all fields are filled up.
If Wbb.Sheets("Report").TextBox3.Value = "" Then
MsgBox "Please check missing data."
Else
'Check if drive is accessible, if not prompt a message
If Dir(filePath1, vbDirectory) = "" Then 'this is where the run-time error is pointing at
MsgBox "Unable to access drive. Please save file manually."
Exit Sub
Else
'Check if folders exists in drive. If does not exist, create folders.
If filePathCheck <> "" Then
aDirs = Split(filePathCheck, "\")
If Left(filePathCheck, 2) = "\\" Then
iStart = 3
Else
iStart = 1
End If
sCurDir = Left(filePathCheck, InStr(iStart, filePathCheck, "\"))
For i = iStart To UBound(aDirs)
sCurDir = sCurDir & aDirs(i) & "\"
If Dir(sCurDir, vbDirectory) = vbNullString Then
MkDir sCurDir
End If
Next i
End If
End If
Upvotes: 0
Views: 73
Reputation: 8518
Dir()
throws an error if the left part of the directory does not exist. However the FileSystemObject
simply returns False
without throwing an error.
Public Function FolderExists(ByVal Path As String) As Boolean
With CreateObject("Scripting.FileSystemObject")
FolderExists = .FolderExists(Path)
End With
End Function
No reference the the Scripting.Runtime
required.
Upvotes: 2
Reputation: 10139
Going off of what @Jeeped said in your comments, use Error Handling - [1] - [2] - [3]
On Error GoTo ErrHandler
Exit Sub
ErrHandler:
Select Case Err.Number
Case 52
MsgBox "~"
' Possibly pop up a save dialog if you desire
Err.Clear
Resume Next
Case Else
MsgBox "!"
Exit Sub
End Select
Upvotes: 1