vba:捕获一个文件找不到与DIR的exception
我正在使用DIR打开一个文件:
If Dir("some dir" + "some file", vbNormal) <> "" The End If
如果DIR不存在,那么我得到一个例外BAD文件名或数字; 但是,如果目录存在,那么这个IF语句工作正常。
问题在DIR不存在的情况下如何处理exception?
Public Function IsADirectory(ByVal TheName As String) As Boolean If GetAttr(TheName) And vbDirectory Then IsADirectory = True End If End Function
这个怎么样 ?
以下代码处理目标不存在的情况:
Public Function IsADirectory(ByVal TheName As String) As Boolean On Error Resume Next Dim theResult As Boolean theResult = GetAttr(TheName) And vbDirectory If Err.Number = 0 Then IsADirectory = theResult Else MsgBox "The target is not found." End If End Function