Error 70 Permission Denied trying to kill a workbook

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,404
Office Version
  1. 2016
Platform
  1. Windows
I'm having some real issues trying to kill a closed workbook - my routine has a main workbook and what I'm trying to achieve is the following;

1) Get the main workbook name and store it for use later
2) Save the current (main) workbook as a temporary file
3) Kill the original main workbook so I can open another (updated) workbook and save it as the original main file name

This is what I have so far:

VBA Code:
SavedFileName = ActiveWorkbook.Name
Application.ScreenUpdating = False
Application.EnableEvents = False
ThisWorkbook.SaveAs filename:=ThisWorkbook.Path & "\Temp.xlsm"
Application.EnableEvents = True
SetAttr ThisWorkbook.Path & "\Temp.xlsm", vbHidden
SetAttr ThisWorkbook.Path & "\" & SavedFileName, vbNormal
Kill ThisWorkbook.Path & "\" & SavedFileName
Application.ScreenUpdating = True
DoEvents

The workbooks are stored in a network folder and there are NO permission issues as the 'temp' file is deleted without an issue in another routine later. I am getting a persistent 'Error 70 Permission Denied' on the following line:

VBA Code:
Kill ThisWorkbook.Path & "\" & SavedFileName

I have tried various things to no avail - can anyone spot where the issue is?
 
Hi guys - I'm afraid removing the 'SetAttr' line has not solved the problem. Just a thought, is there anything I can do to effectively clean any reference in the memory to the original file before I try to kill it?
 
Upvote 0

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Assuming you're trying to delete the workbook the code is running in, and you have sufficient permissions in the directory,

VBA Code:
Sub KillMe()
  With ThisWorkbook
    If Len(Dir(.FullName)) Then
      .Saved = True
      On Error Resume Next
      .ChangeFileAccess Mode:=xlReadOnly
      On Error GoTo 0
      SetAttr Pathname:=.FullName, Attributes:=vbNormal
      Kill .FullName
      .Close SaveChanges:=False
    End If
  End With
End Sub
 
Upvote 0
shg, thanks, I will try this and report back...

Would you mind talking me through each element so I can understand how it works?
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,876
Members
449,056
Latest member
ruhulaminappu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top