在编写vba的解决方案时,经常需要与文件打交道。
FileSystemObject对象提供了一系列关于操作文件的对象、属性和方法。
其中FileExists方法可以用于判断文件是否存在。
以下是一个用FileExists方法判断指定的文本文档是否存在的代码:
Sub qq1722187970()
Excel.Application.ScreenUpdating = False
Excel.Application.Calculation = xlCalculationManual
Excel.Application.DisplayAlerts = False
Dim sFliePath As String
sFliePath = Excel.ThisWorkbook.Path & "/test.txt"
Dim oFSO As Object
Dim oTextStream As Object
Set oFSO = CreateObject("Scripting.FileSystemObject")
With oFSO
'如果存在指定的文件
If .FileExists(sFliePath) Then
'操作代码
Else
'如果存在指定的文件
'操作代码
End If
End With
Excel.Application.ScreenUpdating = True
Excel.Application.Calculation = xlCalculationAutomatic
Excel.Application.DisplayAlerts = True
End Sub


发表评论