VBA检查文件是否存在使用记事本

我想询问社区是否有人可以将文件目录path存储到记事本或其他文档文件中,让VBA扫描每个文件目录并检查文件是否存在。

这是一个使用dir来说明上面的注释的注释代码。

 'This function will check if the file or the directory 'exists and returns a boolean. Function ExistTest(Path As String) As Boolean 'The function default return is set to false ExistTest = False 'Remove stream reader appending Path = Replace(Path, "", "") 'Dir returns a 0-length string if the file or directory doesn't exist If Len(Dir(Path)) > 0 Or Len(Dir(Path, vbDirectory)) > 0 Then ExistTest = True End If End Function Sub main() 'Defining the source file Dim SourceFile As String: SourceFile = "d:/t.txt" 'Open a stream reader Open SourceFile For Input As #1 'Read all line until the end of file 'Before we define a temporary string the path as we go through 'the sourcefile Dim TempPath As String Do Until EOF(1) Line Input #1, TempPath 'Debug print the results Debug.Print (TempPath & " " & ExistTest(TempPath)) Loop Close #1 End Sub 

Debug.Print将在即时窗口中打印。 要启用该function,请转到查看>立即窗口