I found some code that allows our office to send files to SharePoint (see below). For the most part it works just fine. However if an error occurs with Sharepoint, like the wrong password was supplied, we get no indication and the macro continues to run. It's not until we actually go to Sharepoint and look through the folders do we find that nothing was ever uploaded.
Is there any way to either reverse this code so it can read the files back from Sharepoint after completion OR incorporate some type of error response from Sharepoint as each file is sent?
The code used to send files up is as follows (and it works):
Private Sub copyToSharePoint(sharepointURL As String, filePath As String)
On Error GoTo errhandler
'sharePointUrl should not end in a "/"
'Initialize Variables
Dim LlFileLength As Long
Dim Lvarbin() As Byte
Dim LobjXML As Object
Dim LvarBinData As Variant
Dim LstrFileName As String, PstrFullfileName As String, PstrTargetURL As String
Dim fileName As String, lenFileName As Long
Password = "xxxxxx"
UserName = "xxxxxx"
'Extract file name
lenFileName = Len(filePath) - InStrRev(filePath, "\")
fileName = Right(filePath, lenFileName)
'Check that the webUrl ends in an "/"
If Right(sharepointURL, 1) <> "/" Then
sharepointURL = sharepointURL & "/"
End If
'**************************** Upload binary files *****************
Set LobjXML = CreateObject("Microsoft.XMLHTTP")
PstrFullfileName = filePath
LlFileLength = FileLen(PstrFullfileName) - 1
' Read the file into a byte array.
ReDim Lvarbin(LlFileLength)
Open PstrFullfileName For Binary As #1
Get #1, , Lvarbin
Close #1
' Convert to variant to PUT.
LvarBinData = Lvarbin
PstrTargetURL = sharepointURL & fileName
' Put the data to the server; false means synchronous. 'username & password added ++++
LobjXML.Open "PUT", PstrTargetURL, False, UserName, Password
' Send the file in.
LobjXML.Send LvarBinData
Set LobjXML = Nothing
On Error GoTo 0
Exit Sub
errhandler:
If Err.Number = 53 Then
MsgBox "Excel was unable to create the HR file to submit to SharePoint. " & vbNewLine & _
"Please check that you are not running out of disk space and that no MS Office add-in is causing issues with Excel.", vbCritical, "File Error"
Exit Sub
Else
MsgBox "Your HR could not be submitted to SharePoint. The following error occurred:" & vbNewLine & _
"Error " & Err.Number & ": " & Err.Description, vbCritical, "Error Uploading to SharePoint"
Exit Sub
End If
End Sub
Bookmarks