aaa
aaa

Reputation: 858

VBA - runtime error 52

I'm getting the error

run time error 52, Bad file name or number

while using FileCopy to copy local file to sharepoint.

I follow the document from microsoft and the file path and the file name several times but could find the error.

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.ThisWorkbook.path
myPath = Application.ThisWorkbook.FullName
MsgBox "This is the folderPath" & folderPath 'C:\Users\username\Desktop
MsgBox "This is the filepath" + myPath 'C:\Users\username\Desktop\testing.xlsm

SharePointLib = "Z:\Test Folder - New Format\"
copyPath = folderPath + "\copyPath\"
MsgBox "The copyPath is = " & copyPath 'C:\Users\username\Desktop\copyPath\

If Not FolderExists(copyPath) Then
    FolderCreate (copyPath)
End If

MsgBox "The file will be uploaded to this address: " + SharePointLib 'Z:\Test Folder - New Format\

ThisWorkbook.SaveCopyAs copyPath & "testing.xlsm"
Call FileCopy(copyPath & "testing.xlsm", SharePointLib)

Exit Sub

loadFailed:
UploadToSharepoint = False

End Sub

I copy the path from window explorer after I map the drive to the SharePoint Site.

drive

Update

Map the drive and get the new Path Z:\Test Folder - New Format, but still hit run time error 52

Upvotes: 0

Views: 9786

Answers (1)

aaa
aaa

Reputation: 858

Holy moly, thanks for the help guys, @newacc2240 was right. I found out the problem after looking at this example.

FileCopy "C:\Source\test.txt", "C:\Destination\test.txt" 'Result: Copies file from "C:\Source" to "C:\Destination"

So for my just need to add my file name to the SharePointLib.

Previously I assumed that it would copy the file from the source to the destination with the file name attached, which cause me to stress up for hours.

Upvotes: 2

Related Questions