Home Permission denied error when trying to delete a file
Reply: 0

Permission denied error when trying to delete a file

Kam
1#
Kam Published in 2018-01-12 15:45:04Z

I am admin to my computer. I am logged into network. The below procedure runs and crashes after line 'XXXXXXXXXXXXXXXXX: stating the permission is denied, at objFile.Delete. Why is permission denied? I am able to delete files and folders from windows explorer. Why can't the vba program delete it? Any solutions? Thanks

Sub RecursiveFolderDelete(MyPath As String)

    Dim FileSys As FileSystemObject
    Dim objFolder As Folder
    Dim objSubFolder As Folder
    Dim objFile As File


    Set FileSys = CreateObject("Scripting.FileSystemObject")
    Set objFolder = FileSys.GetFolder(MyPath)

    For Each objFile In objFolder.Files
        If Left(objFile.Name, 1) <> "~" And objFile.Name <> ThisWorkbook.Name Then
'XXXXXXXXXXXXXXXXX:       
     objFile.Delete
        End If
    Next objFile


    Dim Count As Integer
    Count = 0
    For Each objSubFolder In objFolder.SubFolders
        Count = Count + 1
        RecursiveFolderDelete MyPath & "\" & objSubFolder.Name
    Next objSubFolder

    On Error GoTo endx:

    If Count = 0 Then
        RmDir MyPath
    Else
        If objFolder.SubFolders.Count = 0 Then
            RmDir MyPath
        End If
    End If

endx:
    On Error GoTo 0


    Set FileSys = Nothing
    Set objFolder = Nothing
    Set objSubFolder = Nothing
    Set objFile = Nothing

End Sub
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.456318 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO