VBA – 运行时错误1004

我正尝试使用VBA将本地xlsm文件上载到SharePoint。 用户保存文件后,该文件将自动上传到SharePoint。 这个概念是在用户保存xlsm文件之后,它会创build一个包含复制文件的新文件夹,并将复制的file upload到SharePoint中。 上传后,该文件夹将被删除。

但是,我无法将文件保存到该文件夹

运行时错误1004

我检查可能的原因,文件名/path确实存在,文件夹被成功创build。

该文件没有被另一个程序使用,只有excel运行该文件。

该文件夹是新创build的,它是一个空文件夹,不包含具有相同名称的文件。

在这里输入图像说明

我没有检查所有的path,他们都是正确的。

这是我的代码

Private Sub Workbook_AfterSave(ByVal Success As Boolean) Dim UploadToSharepoint As Boolean Dim SharePointLib As String Dim myPath As String Dim folderPath As String Dim objNet As Object Dim FS As Object Dim copyPath As String Dim copyFilePath As String folderPath = Application.ActiveWorkbook.path myPath = Application.ActiveWorkbook.FullName SharePointLib = "https://company.com/folder/subfoler" copyPath = folderPath + "\copyPath" MsgBox "This is the folderPath = " & folderPath & vbNewLine & "This is the filepath = " & myPath & vbNewLine & "The copyPath is = " & copyPath If Not FolderExists(copyPath) Then FolderCreate (copyPath) End If SharePointLib = SharePointLib & FileNameWithExt(myPath) ThisWorkbook.SaveCopyAs (copyPath) Exit Sub loadFailed: UploadToSharepoint = False End Sub 

任何帮助和build议表示赞赏。 先谢谢了。

您的copyPath只是一个文件夹,但SaveCopyAs的参数应该是带文件名的完整path。

试试这个: ThisWorkbook.SaveCopyAs copyPath & "\filename.xlsx"