構文
Object.FileExists(filespec)
filespecに指定したフルパスで指定したファイル名が存在するかどうか確認します。
FileExistsメソッドのサンプルコード
Public Sub sample()
'■FileSystemObjectの宣言
Dim fso As Object: Set fso = CreateObject("Scripting.FileSystemObject")
'■ファイルが存在するかどうかチェック
Debug.Print fso.FileExists("C:\vba\sample.xlsx") ' 存在すればTrue / しなければFalse
'■IF文で記載する場合は以下。
If fso.FileExists("C:\vba\sample.xlsx") Then
Debug.Print "True"
Else
Debug.Print "False"
End If
'■フォルダの存在確認はFileExistsでは不可。FolderExists
Debug.Print fso.FileExists("C:\vba")
End Sub
注意点
関連記事
- 特にありません。
コメント