vb中相对路径下的文件不存在的时候,怎样的代码能提示文件不存在,并且避免窗体的直接结束?谢谢

来源:百度知道 编辑:UC知道 时间:2024/06/25 16:01:49
相对路径是用了shell函数,比如这段代码:
Private Sub CandyButton6_Click()
Shell App.Path & "\file\mspaint.exe", vbNormalFocus
End Sub

IF Dir(App.Path & "\file\mspaint.exe")="" Then
Msgbox "文件不存在"
Exit sub
End If

Function FileExists(FileName As String) As Boolean
'检查文件是否存在
On Error Resume Next
FileExists = IIf(Dir(Trim(FileName)) <> "", True, False)
End Function

Function DirExists(DirName As String) As Boolean
'检查文件夹是否存在
On Error Resume Next
DirExists = IIf(Dir(Trim(DirName), vbDirectory) <> "", True, False)
End Function

给你提供两个函数,自己试着调用吧

if dir("Path") then msgbox "Error"

用DIR 函数判断文件是否存在先。

用dir