运行时错误70权限在将文件移动到另一个文件夹时被拒绝

时间:2016-11-14 17:30:29

标签: excel vba excel-vba

我试图将所有excel文件从一个文件夹路径移动到另一个文件夹路径,并且我收到了权限被拒绝错误。这是我的代码:

Sub Move_Certain_Files_To_New_Folder()

    Dim FSO As Object
    Dim FromPath As String
    Dim ToPath As String
    Dim FileExt As String
    Dim FNames As String

    FromPath = "R:\FromPath\Files"  '<< Change
    ToPath = "R:\ToPath\Backup" & Format(Now, "yyyy-mm-dd h-mm-ss") 

    FileExt = "*.xl*"

    If Right(FromPath, 1) <> "\" Then
        FromPath = FromPath & "\"
    End If

    FNames = Dir(FromPath & FileExt)
    If Len(FNames) = 0 Then
        MsgBox "No files in " & FromPath
        Exit Sub
    End If

    Set FSO = CreateObject("scripting.filesystemobject")

    FSO.CreateFolder (ToPath)

    FSO.MoveFile Source:=FromPath & FileExt, Destination:=ToPath
    MsgBox "You can find the files from " & FromPath & " in " & ToPath

End Sub

错误来自这部分:FSO.MoveFile来源:= FromPath&amp; FileExt,Destination:= ToPath 感谢任何帮助! :)

1 个答案:

答案 0 :(得分:1)

移动文件无法独占访问该文件。如果另一个进程打开了(这会导致你得到的运行时错误70),除了尝试移动下一个文件之外,你可以做的不多,并且稍后再次尝试该锁定文件。 / p>

或者您可以复制到目的地。但是,无论文件打开了什么,都可能意味着要对其进行更改,而且您的副本不会拥有它们。