VBA - 运行时错误1004

时间:2017-11-22 07:51:46

标签: excel vba sharepoint

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

但是,由于

,我无法将文件保存到文件夹中
  

运行时错误1004

我检查可能的原因,文件名/路径确实存在,文件夹已成功创建。

该文件未被其他程序使用,只有excel运行该文件。

该文件夹是新创建的,它是一个空文件夹,不包含同名文件。

enter image description here

我确实检查了所有路径并且它们都是正确的。

这是我的代码

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

感谢任何帮助和建议。谢谢你提前。

1 个答案:

答案 0 :(得分:4)

您的copyPath只是一个文件夹,但SaveCopyAs的参数应该是包含文件名的完整路径。

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