+ Reply to Thread
Results 1 to 6 of 6

fso.DeleteFolder works first time, not second

  1. #1
    Registered User
    Join Date
    04-12-2011
    Location
    Leeds, England
    MS-Off Ver
    Excel 2007/2010
    Posts
    67

    fso.DeleteFolder works first time, not second

    Hi,

    I'm pulling my hair out with this problem and would appreciate some help. I'm pretty sure that the first time I run my program it is keeping the folder or file in use somehow so the second time I run it the deletefolder command won't work.

    Unfortunately the code is pretty lengthy and hard to follow so I'll just try and describe it for now:

    If the folder exists I delete it with fso.DeleteFolder then recreate it and make a few files in it. I use fso objects and also set an object for each file but I've tried setting them to nothing at the end with:
    Please Login or Register  to view this content.

    The files/folder is not open when I run the second time but still it won't work so I must be missing something. Any advice gratefully received.

    Chinchin.
    Last edited by Leith Ross; 07-01-2011 at 11:50 AM. Reason: Added Code Tags

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: fso.DeleteFolder works first time, not second

    Hello Chinchin,

    After you delete the file or folder assigned to an object variable, you must reassign the object variable to the new instance. If I have a object variable MyFolder that is set to "C:\Test" and I delete it and then recreate the folder "C:\Test", I must reassign MyFolder like this...
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    04-12-2011
    Location
    Leeds, England
    MS-Off Ver
    Excel 2007/2010
    Posts
    67

    Re: fso.DeleteFolder works first time, not second

    Thanks Leith, but I don't think that's the problem because I am just using a string rather than an object for the filepath. This is the procedure I'm calling to delete the folder:

    Please Login or Register  to view this content.
    Is there something else it could be?

    Thanks again,

    Chinchin

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: fso.DeleteFolder works first time, not second

    Hello Chinchin,

    Do you check for the existence of the folder before calling DeleteFolder? It is difficult to asses the problem with so little information.

  5. #5
    Registered User
    Join Date
    04-12-2011
    Location
    Leeds, England
    MS-Off Ver
    Excel 2007/2010
    Posts
    67

    Re: fso.DeleteFolder works first time, not second

    Hi Leith,

    Yes - the problem is actually when I try to recreate the folder it gives error 58 (file already exists) and, when I check, the folder does still exist but with the files gone.

    Here's the portion of the code with some bits stripped out:

    Please Login or Register  to view this content.
    Last edited by Chinchin; 07-01-2011 at 01:03 PM.

  6. #6
    Registered User
    Join Date
    03-25-2004
    Posts
    57

    Re: fso.DeleteFolder works first time, not second

    I have had this problem too, and have found what's probably an inefficient solution, but all sorts of searches on the net for suggested solutions didn't work for me either.

    Like you, I got the error message permission denied the second time I tried to use DeleteFolder command. If I ran the macro again, but this time skipping the first DeleteFolder line, it worked without a problem.

    I thought I'd try to call a new macro after the first DeleteFolder, which would effectively do the work of the second DeleteFolder line, but got the same error message.

    So, also in frustration, I then tried this which worked. At the end of the macro, I inserted some code to send it back and re-run the macro once more, this time bypassing now irrelevant bits of code and the first DeleteFolder line. For some reason, this works.

    Hope this helps
    Cheers
    free

+ 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