+ Reply to Thread
Results 1 to 8 of 8

Routine for checking if file exists

  1. #1
    Centurion
    Guest

    Routine for checking if file exists

    Hello,
    I am using Excel 97, and have written (by recording keystrokes)
    a macro that does several things and finishes off by saving the file.
    However if the saved file already exists it stops the macro and prompts
    user if it's ok to overwrite the file.

    I would like to........

    1) how do I test (within VBA) to see if the file exists.

    2) If the file does exist, then overwrite without prompt.
    Or alternately, save file and incrementally add '1'
    to the end of the subsequent file.

    Example
    File-01
    File-02
    File-03


    Thanks in anticipation.

    --
    Centurion



  2. #2
    ben
    Guest

    RE: Routine for checking if file exists

    several ways
    you could use the
    Kill
    command to kill the file first
    and then save it so that there is no file by that name
    you will get an error on the Kill command if the file does not exist, but
    you can use
    On error resume next
    to skip the error. Look up in help index on the KILL method.
    Warning. This will permantly delete any file you use it on without asking,
    and can delete entire folders!

    "Centurion" wrote:

    > Hello,
    > I am using Excel 97, and have written (by recording keystrokes)
    > a macro that does several things and finishes off by saving the file.
    > However if the saved file already exists it stops the macro and prompts
    > user if it's ok to overwrite the file.
    >
    > I would like to........
    >
    > 1) how do I test (within VBA) to see if the file exists.
    >
    > 2) If the file does exist, then overwrite without prompt.
    > Or alternately, save file and incrementally add '1'
    > to the end of the subsequent file.
    >
    > Example
    > File-01
    > File-02
    > File-03
    >
    >
    > Thanks in anticipation.
    >
    > --
    > Centurion
    >
    >
    >


  3. #3
    Chip
    Guest

    Re: Routine for checking if file exists

    I could help you with either of those options, but another option is
    simply to have it not say that prompt...it could just save over if it
    exists, or save it as a new file if it doesnt-all without
    asking...would that be better for you?


  4. #4
    ben
    Guest

    Re: Routine for checking if file exists

    I have always thought there was a way to do that. How do you ahve it
    overwrite without asking? because Application.Displayalerts = False has
    never worked for me

    "Chip" wrote:

    > I could help you with either of those options, but another option is
    > simply to have it not say that prompt...it could just save over if it
    > exists, or save it as a new file if it doesnt-all without
    > asking...would that be better for you?
    >
    >


  5. #5
    Centurion
    Guest

    Re: Routine for checking if file exists

    "Chip" <[email protected]> wrote in message
    news:[email protected]...
    > I could help you with either of those options, but another option is
    > simply to have it not say that prompt...it could just save over if it
    > exists, or save it as a new file if it doesnt-all without
    > asking...would that be better for you?
    >


    Yes, how do I do that.

    --
    Centurion



  6. #6
    Chip
    Guest

    Re: Routine for checking if file exists

    Hmm..that is odd..it has always worked for me...can you give me your
    code, and i'll test it....


  7. #7
    BBert
    Guest

    Re: Routine for checking if file exists

    On Wed, 26 Jan 2005 18:57:59 GMT, Centurion wrote...

    > 1) how do I test (within VBA) to see if the file exists.


    Sub FileExists()
    Dim fFileExists As Boolean

    fFileExists = (Len(Dir("C:\config.sys")) > 0)
    If fFileExists = True Then
    msgbox "File Exists"
    Else
    msgbox "No File Found"
    End If
    End Sub

    --
    Met vriendelijke groeten / Mit freundlichen Grüßen / With kind
    regards/Avec mes meilleures salutations
    BBert

    April 20, 1986
    Celtics (135) - Bulls (131)
    Larry Bird: "God disguised as Michael Jordan"

  8. #8
    Forum Contributor
    Join Date
    01-21-2005
    Location
    Colorado
    MS-Off Ver
    2000,2003,2007
    Posts
    481

    Display Alerts

    I was also trying to set the Application.DisplayAlerts to False but it did not seem to be working.

    However, when I simply used DisplayAlerts = Flase (without the preceeding Application. ) it worked as expected.

    Has anyone else encountered this?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1