Using code to delete files of a different application

bknight
BronzeLounger
Posts: 1482
Joined: 08 Jul 2016, 18:53

Using code to delete files of a different application

Post by bknight »

I have a spreadsheet that has some buttons on it. One is a button to open Excel as administrator, so the files can be deleted with code. I can do this manually, but it takes time. The code has worked in the past but when I select the delete old files, I get an error permission denied. I'll post the file and maybe the code could be changed to allow permission.
I checked that I as user had full permissions of that application.
After that I get can't locate object errors. The files are in the exact location as before so I'm bewildered.
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 79020
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Using code to delete files of a different application

Post by HansV »

if you click Debug when an error occurs, which line is highlighted when you get the "Permission denied" error? And which line is highlighted when you get the "Cannot locate object" error?
Best wishes,
Hans

bknight
BronzeLounger
Posts: 1482
Joined: 08 Jul 2016, 18:53

Re: Using code to delete files of a different application

Post by bknight »

I had shut the machine down, earlier. So started up again and ran the macro without errors and deleted the files as expected. That being said the line which invoked the permission denied occurred at

Code: Select all

        If LCase(objFile.Name) Like "*.tsrslts" Then
            If objFile.DateLastModified < Date - 60 Then
                'objFile.Delete.Permission = True
                objFile.Delete
            End If
        End If
objFile.Delete
I'm not sure where the "can't locate object" but it is moot currently, since the code ran.
Why would the error occur? The code worked in the past and then not worked. However by shutting down and starting up worked, again???
Thanks

User avatar
HansV
Administrator
Posts: 79020
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Using code to delete files of a different application

Post by HansV »

I guess a file got locked somehow. Rebooting took care of that.
Best wishes,
Hans

DRUIDINN
NewLounger
Posts: 11
Joined: 16 May 2017, 23:05

Re: Using code to delete files of a different application

Post by DRUIDINN »

It sounds like your Excel macro is hitting a permissions snag when trying to delete files, and you're also seeing "can't locate object" errors. Check folder permissions and make sure your file paths in the code are spot on.

bknight
BronzeLounger
Posts: 1482
Joined: 08 Jul 2016, 18:53

Re: Using code to delete files of a different application

Post by bknight »

See my previous post it ran after shutting down and turning on pc.