'The file you want to check
Const strFile = "C:\testfile.txt"Private Sub Form_Load()
    Dim msg As String
    If ReportFileStatus(strFile) Then
      msg = strFile & " exists."
    Else
      msg = strFile & " doesn't exist."
    End If
   MsgBox msg
End Sub
'Add the Reference of Microsoft Scripting Runtime
Function ReportFileStatus(filespec As String) As Boolean   Dim fso
   Set fso = CreateObject("Scripting.FileSystemObject")
   If (fso.FileExists(filespec)) Then
      ReportFileStatus = True
   Else
      ReportFileStatus = False
   End If
End FunctionFunction ReportFileStatus(filespec As String) As Boolean
   Dim strdir As String
   strdir = Dir(filespec)
   If strdir <> "" Then
      ReportFileStatus = True
   Else
      ReportFileStatus = False
   End If
End Function

解决方案 »

  1.   

    FileExists方法 返回 描述如果指定的文件存在,返回 True,若不存在,则返回 False。语法object.FileExists(filespec) FileExists 方法语法有如下几部分:部分 描述 
    object 必需的。始终是一个 FileSystemObject 的名字。 
    filespec 必需的。要确定是否存在的文件的名字。如果认为文件不在当前文件夹中,必须提供一个完整的路径说明(绝对的或相对的)。 
      

  2.   

    楼上的方法使用FileSystemObject,我的方法不要:
    Public Function FileIsExist(Filename As String, Optional Attribus As VbFileAttribute = vbArchive) As Boolean
        Dim cName As String
        cName = Dir(Filename, Attribus)
        FileIsExist = (Format(cName, ">") = Format(StripFile(Filename), ">"))
    End FunctionPublic Function StripFile(Name As String) As String
        Dim pos As Integer, pos1 As Integer
        pos = InStr(Name, "\")
        While pos <> 0
            If pos <> 0 Then pos1 = pos
            pos = InStr(pos + 1, Name, "\")
        Wend
        StripFile = Right(Name, Len(Name) - pos1)
    End Function
      

  3.   

    最简单的办法是用dir("文件.后缀"),如果函数返回"",则“文件.后缀”不存在。如果还要对目录或者驱动器进行其他操作,那就看楼上的吧。