I am trying to make a copy of a PDF file from a network drive (sourceFile
) to the user's Temp folder but there is a good chance that the path of sourceFile
is longer than 260 characters.
I tried to adopt ChipsLetten's answer in this question and modify it to perform CopyFile
but it returns Error 5: Invalid procedure call or argument
.
Private Sub Test_CopyFile()
Const testfolder As String = "\\servername\folder1\folder2\folder3\folder4\folder5\folder6"
Const testfile As String = "superlongfilename.pdf"
Const sourceFile As String = testfolder & "\" & testfile 'Actual test path is 280 characters long
Dim destinationPath As String: destinationPath = Environ("Temp") & "\" & testfile
Debug.Print LongFileCopy(sourceFile, destinationPath)
End Sub
Private Function LongFileCopy(pathCopy As String, pathDestination As String) As Boolean
Dim fsoObject As Scripting.FileSystemObject
Const MAX_PATH_LENGTH As Integer = 260
If Len(pathCopy) > MAX_PATH_LENGTH Then
pathCopy = "\\?\UNC\" & Mid$(pathCopy, 3)
End If
Set fsoObject = New Scripting.FileSystemObject
If fsoObject.FileExists(pathCopy) Then
fsoObject.CopyFile pathCopy, pathDestination, True 'Error 5: Invalid procedure or argument at this line
LongFileCopy = True
End If
End Function
Does anyone have any solution to this? Thanks in advance!