捕获/修复运行时错误

时间:2018-02-09 05:40:45

标签: excel vba excel-vba

  

运行时错误'52':错误的文件名或编号

我想请求您的帮助和建议,当我使用的计算机无法真正访问目录时,我的代码遇到“运行时错误'52':文件名或编号错误”驾驶。我在我的个人计算机上试过它,它显示了运行时错误。

当我使用可以访问目录驱动器的公司计算机时,我的代码工作正常。如果我尝试更改文件夹名称使其无法访问(出于故障排除目的),它会显示消息框“无法访问”。

我想要做的是实际显示一个消息框,以防所用的计算机无法访问该目录。

我尝试搜索,尝试使用“”,0和vbNullString。但没有成功。提前感谢您的帮助。

'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

2 个答案:

答案 0 :(得分:2)

如果目录的左侧部分不存在,

public void onClick(View view) { youTubePlayer.release(); startActivity(new Intent(this, MainActivity.class)); } @Override protected void onRestart() { super.onRestart(); youtubePlayerView.initialize(YOUTUBE_API_KEY, this); } 将引发错误。但是,Dir()只会返回FileSystemObject而不会抛出错误。

False

不参考所需的Public Function FolderExists(ByVal Path As String) As Boolean With CreateObject("Scripting.FileSystemObject") FolderExists = .FolderExists(Path) End With End Function

答案 1 :(得分:1)

取消评论中@Jeeped所说的内容,使用错误处理 - [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