我正在尝试使用 VBA 将本地 xlsm 文件上传到 SharePoint Online。用户保存文件后,文件将自动上传到 SharePoint。
这个概念是在用户保存 xlsm 文件后,它会使用复制的文件创建一个新文件夹,并将文件夹中的复制文件上传到 SharePoint。上传后,该文件夹将被删除。
但是,由于以下原因,我无法将文件保存到文件夹中
Runtime error 1004
我检查了可能的原因,文件名/路径确实存在,文件夹已成功创建。
该文件未被其他程序使用,只有 excel 运行该文件。
该文件夹是新创建的,它是一个空文件夹,不包含同名文件。
我确实检查了所有路径,它们都是正确的。
这是我的代码
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
最佳答案
您的 copyPath
只是一个文件夹,但是SaveCopyAs
的参数应该是带有文件名的完整路径。
尝试这个:ThisWorkbook.SaveCopyAs copyPath & "\filename.xlsx"
关于excel - 尝试将文件上传到 SharePoint 时出现运行时错误 1004,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47429362/