0

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 
3
  • 4
    use on error goto ... Commented Feb 9, 2018 at 5:47
  • 1
    Can you please show an example what filePath1 contains if the error occurs? The only thing I can think why this error is thrown is if filePath1 contains an UNC path like "\\Servername\\SharedFolder\Resource\" and this path is not available over the network. It this is true for you, see answers.microsoft.com/en-us/msoffice/forum/…. Commented Feb 9, 2018 at 5:59
  • @AxelRichter Yes, you are correct that filepath1 is UNC path which is \\bmptdat085\Working\Users\. Thank you for the link. It works great as well. Commented Feb 12, 2018 at 13:05

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer. I am accepting this as the best answer as I have researched that this way is the fastest and most efficient way to check if a network drive is available. It works great.
1

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 

1 Comment

Thank you for the detailed and informative solution. I learned a lot in error handling in VBA. It works okay with my macro. Thanks again.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.