Proper formatting of the SharePoint destination EXCEL – VBA
The code supplied works properly, if my destination is a local PC file. However, I have tried various suggestions and a common error code is 76, meaning there is something wrong with the path, or permissions or …
I have tried the map it to a network drive trick, but receive access denied and on top of that, there will be various people making use of this spreadsheet that are not going to know anything about mapping drives.
I have to assume there is a standard method of stating the path for sharepoint URL or even the network style. I have tried many, just not going my way.
I have no issue going to the sharepoint site and uploading the same file. I would think permissions would kick in there as well.
Do you have a working model I can use?
sharePointURL = “https://mycompany.sharepoint.com/sites/OnBO/Shared%20Documents/OBO_QA_Export_Files/“
sharePointURL = “\mycompany.sharepoint.comsitesOBOShared%20DocumentsOBO_QA_Export_Files”
sharePointURL = “C:UsersmyusernameDownloads” ‘ this one works fine
————–
Sub CopyFileToSharePoint(localFilePath As String, fileName As String)
Dim sharePointURL As String
Dim targetFilePath As String
Dim fso As Object
On Error GoTo ErrorHandler
‘ Set the SharePoint URL
‘sharePointURL = “https://mycompany.sharepoint.com/sites/OnBO/Shared%20Documents/OBO_QA_Export_Files/“
sharePointURL = “\mycompany.sharepoint.comsitesOBOShared%20DocumentsOBO_QA_Export_Files”
‘sharePointURL = “C:UsersmyusernameDownloads” ‘ this one works fine
‘ Set the target file path
‘targetFilePath = sharePointURL & fileName
targetFilePath = Replace(sharePointURL, ” “, “%20”)
‘ Create FileSystemObject
Set fso = CreateObject(“Scripting.FileSystemObject”)
‘ Copy the file to SharePoint
Debug.Print localFilePath, targetFilePath
fso.CopyFile localFilePath, targetFilePath, True
‘ Clean up
Set fso = Nothing
‘ Notify the user that the export is complete
MsgBox “File ” & targetFilePath & ” has been exported to SharePoint ” & sharePointURL & ” “, vbInformation
Exit Sub
ErrorHandler:
Select Case Err.Number
Case -2147024672
MsgBox “Problem with source file path or name ‘” & localFilePath & “‘ was not found.”, vbCritical
Case Else
MsgBox “An unexpected error occurred: ” & Err.Description & ” (Error Number: ” & Err.Number & “)”, vbCritical
End Select
End Sub
The code supplied works properly, if my destination is a local PC file. However, I have tried various suggestions and a common error code is 76, meaning there is something wrong with the path, or permissions or … I have tried the map it to a network drive trick, but receive access denied and on top of that, there will be various people making use of this spreadsheet that are not going to know anything about mapping drives. I have to assume there is a standard method of stating the path for sharepoint URL or even the network style. I have tried many, just not going my way. I have no issue going to the sharepoint site and uploading the same file. I would think permissions would kick in there as well. Do you have a working model I can use? sharePointURL = “https://mycompany.sharepoint.com/sites/OnBO/Shared%20Documents/OBO_QA_Export_Files/”sharePointURL = “\mycompany.sharepoint.comsitesOBOShared%20DocumentsOBO_QA_Export_Files” sharePointURL = “C:UsersmyusernameDownloads” ‘ this one works fine————–Sub CopyFileToSharePoint(localFilePath As String, fileName As String)Dim sharePointURL As StringDim targetFilePath As StringDim fso As ObjectOn Error GoTo ErrorHandler’ Set the SharePoint URL’sharePointURL = “https://mycompany.sharepoint.com/sites/OnBO/Shared%20Documents/OBO_QA_Export_Files/”sharePointURL = “\mycompany.sharepoint.comsitesOBOShared%20DocumentsOBO_QA_Export_Files”‘sharePointURL = “C:UsersmyusernameDownloads” ‘ this one works fine’ Set the target file path’targetFilePath = sharePointURL & fileNametargetFilePath = Replace(sharePointURL, ” “, “%20”)’ Create FileSystemObjectSet fso = CreateObject(“Scripting.FileSystemObject”)’ Copy the file to SharePointDebug.Print localFilePath, targetFilePathfso.CopyFile localFilePath, targetFilePath, True’ Clean upSet fso = Nothing’ Notify the user that the export is completeMsgBox “File ” & targetFilePath & ” has been exported to SharePoint ” & sharePointURL & ” “, vbInformationExit SubErrorHandler:Select Case Err.NumberCase -2147024672MsgBox “Problem with source file path or name ‘” & localFilePath & “‘ was not found.”, vbCriticalCase ElseMsgBox “An unexpected error occurred: ” & Err.Description & ” (Error Number: ” & Err.Number & “)”, vbCriticalEnd SelectEnd Sub Read More