ファイル ファイルが存在しているかどうかを確認する

※より実装に近く表示させる為、コードの改行を避けています。スマホ等で閲覧される際は向きを変えてご覧ください。

※実装するバージョンによってはバージョンアップの仕様により動作しないコードもあります。実装には動作確認の上ご使用下さい。

Option Explicit


Public Function FileExistence(FilePath As StringAs Boolean
'**************************************************
'ファイルが存在しているかどうかを確認する
'**************************************************
    If Dir(FilePath) <> "" Then
        FileExistence = True
    Else
        FileExistence = False
    End If

End Function


Public Function FileExistenceFSO(FilePath As StringAs Boolean
'**************************************************
'ファイルが存在しているかどうかを確認する(FSO)
'**************************************************

    With CreateObject("Scripting.FileSystemObject")
        If .FileExists(FilePath) Then
            FileExistenceFSO = True
        Else
            FileExistenceFSO = False
        End If
    End With

End Function


Private Sub test()
Dim i As String
i = ThisWorkbook.Path & "\"
Debug.Print FileExistence(i & "*.xls")
Debug.Print FileExistence(i & "test.xls")
Debug.Print FileExistence(i)
Debug.Print FileExistence("test.xls")
Debug.Print FileExistence("xls")

'True
'False
'True
'False
'False
End Sub

 

2000年01月01日|[VBサンプルコード]:[ファイル]