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.
Using code to delete files of a different application
-
- BronzeLounger
- Posts: 1504
- Joined: 08 Jul 2016, 18:53
Using code to delete files of a different application
You do not have the required permissions to view the files attached to this post.
-
- Administrator
- Posts: 79321
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Using code to delete files of a different application
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
Hans
-
- BronzeLounger
- Posts: 1504
- Joined: 08 Jul 2016, 18:53
Re: Using code to delete files of a different application
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
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
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
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
-
- Administrator
- Posts: 79321
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Using code to delete files of a different application
I guess a file got locked somehow. Rebooting took care of that.
Best wishes,
Hans
Hans
-
- NewLounger
- Posts: 12
- Joined: 16 May 2017, 23:05
Re: Using code to delete files of a different application
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.
-
- BronzeLounger
- Posts: 1504
- Joined: 08 Jul 2016, 18:53
Re: Using code to delete files of a different application
See my previous post it ran after shutting down and turning on pc.