+ Reply to Thread
Results 1 to 8 of 8

Killing a file that is open

  1. #1
    Registered User
    Join Date
    11-30-2010
    Location
    Chicago
    MS-Off Ver
    Excel 2003
    Posts
    4

    Killing a file that is open

    I am having nightmares with this issue so I was wondering if someone could suggest something.

    Here is my macro:

    Please Login or Register  to view this content.
    Basically I have a spreadsheet linked to get live prices of a stock. Every time the stock price changes, I call this macro and it replaces the current file with a new file containing the stock price. I have another program in C# that is reading this file every time I click a button. I have had tons and tons of problems with deleting files that are currently in use so I added the ErrorHandler. This fixes the problem most of the time, but once in a while it still throws an error in the ErrorHandler.

    What am I doing wrong?

    Thanks for your help
    -Jeff

  2. #2
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Killing a file that is open

    If the file is in use then you'll get the error when you try to kill it in the errorhandler so you either need an on error resume next to skip the error, or a loop to keep trying until it is killed.
    Remember what the dormouse said
    Feed your head

  3. #3
    Registered User
    Join Date
    11-30-2010
    Location
    Chicago
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Killing a file that is open

    Does this look better?

    Please Login or Register  to view this content.

  4. #4
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Killing a file that is open

    I don't think so as your DeleteFile will simply ignore any errors and then you may end up trying to open the file while it's in use. You need to exit the main sub if the delete fails, presumably?

  5. #5
    Registered User
    Join Date
    11-30-2010
    Location
    Chicago
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Killing a file that is open

    But doesn't the On Error GoTo 0 make it keep going through the function until it successfully deletes the file?

  6. #6
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Killing a file that is open

    No, that just cancels the On Error Resume Next. It actually serves no purpose there since normal error handling resumes when the routine terminates anyway.

  7. #7
    Registered User
    Join Date
    11-30-2010
    Location
    Chicago
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Killing a file that is open

    Ok I see. So I changed the deleteFile function to return whether or not it was successful, and then if it wasnt successful the first function quits. Does this sound better?

    Thanks for all your help.

    Please Login or Register  to view this content.

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Killing a file that is open

    Try this:

    Please Login or Register  to view this content.
    E.g., (use with caution)

    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

+ 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