首页 > 编程语言 > 详细

vba判断文件是否存在的两种方法(转)

时间:2019-03-30 11:09:14      阅读:235      评论:0      收藏:0      [点我收藏+]

方法1. 用VBA自带的dir()判断,代码如下:

在 Microsoft Windows 中, Dir 支持多字符 (*)和单字符 (?) 的通配符来指定多重文件

Function IsFileExists(ByVal strFileName As String) As Boolean
    If Dir(strFileName, 16) <> Empty Then
        IsFileExists = True
    Else
        IsFileExists = False
    End If
End Function
 
Sub Run()
    If IsFileExists("D:\vba\abc.txt") = True Then
     文件存在时的处理
        MsgBox "文件存在!"
    Else
     文件不存在时的处理
        MsgBox "文件不存在!"
    End If
End Sub

方法2. 用Windows的文件系统函数进行判断,代码如下:

Function IsFileExists(ByVal strFileName As String) As Boolean
    Dim objFileSystem As Object
 
    Set objFileSystem = CreateObject("Scripting.FileSystemObject")
    If objFileSystem.fileExists(strFileName) = True Then
        IsFileExists = True
    Else
        IsFileExists = False
    End If
End Function
 
Sub Run()
    If IsFileExists("D:\vba\abc.txt") = True Then
     文件存在时的处理
        MsgBox "文件存在!"
    Else
     文件不存在时的处理
        MsgBox "文件不存在!"
    End If
End Sub

 

vba判断文件是否存在的两种方法(转)

原文:https://www.cnblogs.com/luoye00/p/10625247.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!