Menuへ
ファイル・フォルダの存在確認



シートのコード

Option Explicit

'ファイル、フォルダの存在確認
Private Function ExDir(sName As String, nAttr As Integer) As String
    If sName = "" Then
        ExDir = ""
        Exit Function
    End If
On Error Resume Next
    Err.Number = 0
    ExDir = Dir(sName, nAttr)
    If Err.Number <> 0 Then
        ExDir = ""
    End If
On Error GoTo 0
End Function

Private Sub CommandButton1_Click()
    'ファイルの存在確認
    If ExDir("C:\windows\隅田川.bmp", vbNormal) <> "" Then
        CommandButton1.Caption = "有り"
    Else
        CommandButton1.Caption = "無し"
    End If
End Sub

Private Sub CommandButton2_Click()
    'フォルダの存在確認
    If ExDir("C:\windows\", vbDirectory) <> "" Then
        CommandButton2.Caption = "有り"
    Else
        CommandButton2.Caption = "無し"
    End If
End Sub


Topへ