【VBA】判断文件或文件夹是否存在

 
一、判断文件或文件夹是否存在
Function FileFolderExists(strFullPath As String) As Boolean
    On Error GoTo EarlyExit
    If Not Dir(strFullPath, vbDirectory) = vbNullString Then FileFolderExists = True
    EarlyExit:
    On Error GoTo 0
End Function
可判断文件或文件夹是否存在。假如D盘存在1.txt(文件)、新建文件夹(文件夹),则:
msgbox FileFolderExists("D:\1.txt") 'true
msgbox FileFolderExists("D:\1.txt\") 'false
msgbox FileFolderExists("D:\新建文件夹") 'true
msgbox FileFolderExists("D:\test") 'false

二、判断文件夹是否存在
引用于https://www.cnblogs.com/xwdreamer/p/3509701.html
Function FileFolderExists(strFullPath As String) As Boolean
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    If fso.folderExists(strFullPath) Then FileFolderExists = True
    Set fso = Nothing
End Function

 

你可能感兴趣的:(VBA)