zoukankan      html  css  js  c++  java
  • vba判断文件是否存在的两种方法(转)

    方法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:vbaabc.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:vbaabc.txt") = True Then
        ' 文件存在时的处理
            MsgBox "文件存在!"
        Else
        ' 文件不存在时的处理
            MsgBox "文件不存在!"
        End If
    End Sub
  • 相关阅读:
    寒假第七天
    寒假第六天
    寒假第五天
    寒假第四天
    leetcode 105 从前序与中序遍历序列构造二叉树
    leetcode 268 丢失的数字
    leetcode 141 环形链表
    判断顶点是否在三角形内部
    java 基本数据类型
    leetcode 20 有效的括号
  • 原文地址:https://www.cnblogs.com/luoye00/p/10625247.html
Copyright © 2011-2022 走看看