(Solved) – What’s the correct VBA code for uploading a file to sharepoint, not using UNC or drive mapping


I’m trying to find the correct code which allows me to upload files to sharepoint using vba and macros within excel. I’ve found some urlmon code which has solved the file download part of the macro.
I’ve seen a lot of code which focuses on Scripting.FileSystemObject using UNC, winhttp POST and SEND and the SP SDK but I’ve not been able to make the latter work due to site and software install limitations.

I need to be able to directly upload, for e.g to “http://example.com/foldername“. I’ve tried using Scripting.FileSystemObject with the URL but it doesn’t work.

I’m making a bold assumption that there is a valid vba method other than UNC and winhttp POST/SEND for uploading files to sharepoint. Unless of course they are the only two options?

Below is the code I’ve tried to make work, which is copied from someone else’s work on Stack Overflow.

Public Function UploadEICRs(ByVal file As String, uploadFolder As String)

Dim SharepointAddress As String
Dim LocalAddress As String
Dim objNet As Object
Dim FS As Object

' Where you will enter Sharepoint location path
SharepointAddress = "https://example.com/test_folder/"
' Where you will enter the file path, ex: Excel file
LocalAddress = file
SPFolder = SharepointAddress & uploadFolder & "/"

Debug.Print SPFolder

Set objNet = CreateObject("WScript.Network")
Set FS = CreateObject("Scripting.FileSystemObject")

If FS.FileExists(LocalAddress) Then
    FS.CopyFile LocalAddress, SPFolder
End If

Set objNet = Nothing
Set FS = Nothing

End Function

Any help would be greatly appreciated.

Leave a Reply

Your email address will not be published. Required fields are marked *