vbasharepointfso

Upload File to SharePoint through VBA


I've been seeing this code (or similar to this) in some forums but some do not have this lines for mapping: "objNet.MapNetworkDrive “A: ” , SharepointAddress"

Is it safe for the computer/network to omit this code?

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

Solution

  • This might be a better approach:

    Sub UploadToSharepoint()
    
        Dim SharepointAddress As String
        Dim LocalAddress As String
        Dim FS As Object
    
        SharepointAddress = "\\share.deere.com\teams\sm_at_sd\uppcaptracking\Test"
    
        LocalAddress = "c:\blah\blah2\MyWorkFiletoCopy.xlsx"
    
        Set FS = CreateObject("Scripting.FileSystemObject")
    
        If FS.FileExists(LocalAddress) Then
            FS.CopyFile LocalAddress, SharepointAddress
        End If
    
    End Sub
    

    Should not need to map a drive.