簡體   English   中英

VBA-執行階段錯誤1004

[英]VBA - Runtime error 1004

我正在嘗試使用VBA將本地xlsm文件上傳到SharePoint Online。 用戶保存文件后,該文件將自動上傳到SharePoint。 這個概念是在用戶保存xlsm文件之后,它將使用復制的文件創建一個新文件夾,並將該文件夾中的復制文件上載到SharePoint。 上傳后,該文件夾將被刪除。

但是,由於以下原因,我無法將文件保存到文件夾中

運行時錯誤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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM