首页 > 解决方案 > 我的代码无法将文件从 1 个文件夹移动到另一个文件夹

问题描述

我尝试了 2 个不同的代码将文件从 Sharepoint 中的 1 个文件夹移动到另一个文件夹,但收到 2 个不同的错误。请帮忙看看我做错了什么。

错误:运行时错误 5 - 无效的过程调用或参数

Sub MoveAFile()

'Move file to archive after download complete

Dim ogfilepath, newfilepath As String
Dim i As Double

i = 11
Do

    ogfilepath = Worksheets(CN.Name).Cells(i, 2).Value 'https://wwpcis.sharepoint.com/sites/XXX/filename.pdf
    newfilepath = Worksheets(CN.Name).Cells(i, 5).Value
'https://wwpcis.sharepoint.com/sites/XXX/YYY/filename.pdf

    Name ogfilepath As _
    newfilepath

i = i + 1

Loop Until Cells(i, 1).Value = ""
MsgBox ("Moved to archive")

End Sub

错误:运行时错误 76 找不到路径

Sub FSOMoveAllFiles()

'test new code for moving files

    Dim FSO As New FileSystemObject
    Dim FromPath As String
    Dim ToPath As String
    Dim FileInFromFolder As Object

    FromPath = https://wwpcis.sharepoint.com/sites/XXX/filename.pdf
    ToPath = https://wwpcis.sharepoint.com/sites/XXX/YYY/filename.pdf
   
    Set FSO = CreateObject("Scripting.FileSystemObject")

    For Each FileInFromFolder In FSO.GetFolder(FromPath).Files
        FileInFromFolder.Move ToPath
    Next FileInFromFolder

End Sub

标签: excelvba

解决方案


推荐阅读