4

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

3 Answers 3

3

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:")
Sign up to request clarification or add additional context in comments.

Comments

1

Your Quotation marks look a little strange in the area in question : .

Delete and replace them with the ones " that you have used for the shapepointaddress =... variable.

1 Comment

@BrianTompsett-汤莱恩 That seems a little harsh, especially as it seems to identify a very likely cause of the problem, even though the english is not the best,
0

I would expect:

SharepointAddress = "\\share.deere.com\teams\sm_at_sd\suppcaptracking\Test"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.