PDA

View Full Version : check if a file exists



YellOwCn
03-22-2007, 08:44 AM
hi everybody,

I want to check in my macro, if a file exist in a path, but I don't like to open it, because the time can be long to open a big file, so I try to avoid that:
on error goto fileNotExisted
fileopen := path & filename
on error goto 0


Do u have an idea to check it without open file?
thx before

godd day!

dansam
03-22-2007, 08:48 AM
Hi,
Try this:

Public Function FileFolderExists(strFullPath As String) As Boolean
'Author : Ken Puls (www.excelguru.ca (http://www.excelguru.ca/))
'Macro Purpose: Check if a file or folder exists

If Not Dir(strFullPath,vbDirectory) = vbNullString Then FileFolderExists = True

End Function



Public Sub TestFolderExistence()
'Author : Ken Puls (www.excelguru.ca (http://www.excelguru.ca/))
'Macro Purpose: Test if directory exists

If FileFolderExists("F:\Templates") Then
MsgBox "Folder exists!"
Else
MsgBox "Folder does not exist!"
End If

End Sub

Public Sub TestFileExistence()
'Author : Ken Puls (www.excelguru.ca (http://www.excelguru.ca/))
'Macro Purpose: Test if directory exists

If FileFolderExists("F:\Test\TestWorkbook.xls") Then
MsgBox "File exists!"
Else
MsgBox "File does not exist!"
End If

End Sub
'this code was written by ken plus




Hope this helps.

Regards,
Dan

gnod
03-22-2007, 08:49 AM
'Check if the required excel files exist.
Private Sub ChkExcelFiles()
Dim strPath As String

strPath = Dir(ThisWorkbook.Path & "\" & strFilename)

'If there are no Excel files exit the sub
If strPath = "" Then
MsgBox "File not found - " & strFilename, vbCritical
End
End If
End Sub

YellOwCn
03-22-2007, 09:17 AM
thank you, good answer!!:hi:

johnske
03-22-2007, 11:31 AM
Public Function FileExists(FilenameAndPath As String) As Boolean
'Dick Kusleika's technique
FileExists = CBool(Len(Dir(FilenameAndPath)))
End Function