尝试删除文件时权限被拒绝错误

时间:2018-01-12 15:45:05

标签: vba permissions

我是我的电脑管理员。 我登录了网络。 以下过程运行并崩溃 后排 ' XXXXXXXXXXXXXXXXX: 在objFile.Delete声明拒绝许可。 为什么拒绝许可? 我可以从Windows资源管理器中删除文件和文件夹。 为什么vba程序不能删除它? 有解决方案吗 感谢

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

0 个答案:

没有答案
相关问题