Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload document to SharePoint via VBA

I have this code to upload a document to SharePoint via VBA by mapping it to a drive.

I get

"Compile error, Sub or function not defined".

Then it highlights the second quote mark from this line:

objNet.MapNetworkDrive “A: ” , SharepointAddress

Below is the entire subroutine.

Sub UploadToSharepoint()

Dim SharepointAddress As String
Dim LocalAddress As String
Dim objNet As Object
Dim FS As Object
SharepointAddress = "http://share.deere.com/teams/sm_at_sd/suppcaptracking/Test"
LocalAddress = ”c: MyWorkFiletoCopy.xlsx”
Set objNet = CreateObject(“WScript.Network”)
Set FS = CreateObject(“Scripting.FileSystemObject”)
objNet.MapNetworkDrive “A: ” , SharepointAddress

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

objNet.RemoveNetworkDrive “A: ”
Set objNet = Nothing
Set FS = Nothing

End Sub
like image 903
user3845232 Avatar asked Oct 13 '25 02:10

user3845232


1 Answers

I had a similar challenge. Exporting the file was not working. Correcting errors in code formatting and spacing, I created a subroutine that will do this. It is working well on my machine.

This sub takes four arguments: the name of the file, path to where the file is now, the path to the sharepoint folder, and an optional argument for the temporary mapped network drive (in case A is used on a machine).

Public Sub uploadFileToSP(filename As String, localPath As String, sharePath As String, Optional tempdrive As String = "A:")
    Dim ObjNet As Object, FS As Object
    Set ObjNet = CreateObject("WScript.Network")
    Set FS = CreateObject("Scripting.FileSystemObject")
    If FS.FileExists(localPath & Application.PathSeparator & filename) Then
        ObjNet.MapNetworkDrive tempdrive, sharePath

        FS.CopyFile localPath & Application.PathSeparator & filename, tempdrive & Application.PathSeparator & filename
        ObjNet.RemoveNetworkDrive tempdrive
        Set ObjNet = Nothing
    End If
End Sub

Here is an example call to the subroutine

Call uploadFileToSP("myImage.JPG", "D://my/path", "https://my/sharepoint/path", "A:")
like image 95
jessi Avatar answered Oct 14 '25 18:10

jessi