Results 1 to 2 of 2
  1. #1
    2 Star Lounger
    Join Date
    Sep 2001
    Blue Springs, Missouri, USA
    Thanked 0 Times in 0 Posts

    File exists (xl2k)

    I want to programmitcally check to see if a file exists. If it does, then I am going to kill it (I know how to do that). But if the file doesn't exists, then the code gives an error message. I could get around it by using the On Error but I would rather check for the files existance. Any suggestions on how to accomplish this?



    <img src=/S/gramps.gif border=0 alt=gramps width=20 height=20>

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Thanked 28 Times in 28 Posts

    Re: File exists (xl2k)

    The classic Basic way is to use the Dir function. Dir takes a string argument representing a path and file name (which may contain wildcards) and returns the file name of the first file matching the argument, or an empty string if no file was found. So you could use

    Dim strFileName As String
    strFileName = "C:ExcelTest.xls"
    If Dir(strFileName) = "" Then
    MsgBox "File not found", vbExclamation
    ' your code here
    End If

    You can also use the FileSystemObject, it has a method FileExists. You must set a reference to Microsoft Scripting Runtime in order to use it. Be aware that in some corporate network environments, the use of Scripting is disabled.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts