0

I have move my backend to sharepoint and created a folder called Images. The folder exists in the documents folder. The linked string is in the form: https://webname365.sharepoint.com/sites/SECS-Department I would like to upload files to the Images folder in the Documents folder using a file dialog. https://webname365.sharepoint.com/sites/SECS-Department/Shared Documents/Images/ I have looked around and using the REST API seems to be the best method for doing this. I came across https://stackoverflow.com/questions/75285008/vba-upload-of-file-to-sharepoint-using-rest-api-download-already-implemented-in and tried to addapt it but I get Error "404 NOT FOUND" Can someone help me with this?

Private Sub uploadImage_Click()
    Dim ado As Object
    Dim ofd As Object
    Dim filePath As String
    Dim strExt As String
    Dim newFileName As String
    Dim strBackEndPath As String
    Dim i As Integer
    Dim SharePointURL As String

    strBackEndPath = CurrentDb.TableDefs("tblEquipment").Connect
    i = InStrRev(strBackEndPath, "DATABASE=") + Len("DATABASE=")
    
    SharePointURL = Mid(strBackEndPath, i, InStr(i, strBackEndPath, ";") - i) & "/Shared Documents/Images/"
    SharePointURL = Replace(Mid(strBackEndPath, i, InStr(i, strBackEndPath, ";") - i) & "/Shared Documents/Images/", " ", "%20")

    Set ofd = Application.FileDialog(3)
    ofd.AllowMultiSelect = False
    ofd.Show

    If ofd.SelectedItems.Count = 1 Then
        filePath = ofd.SelectedItems(1)
        strExt = Mid(ofd.SelectedItems(1), InStrRev(ofd.SelectedItems(1), "."))

        If strExt = ".jpeg" Then
            strExt = ".jpg"
        End If
        
        newFileName = ReplaceSpecialChars(Me.itemName.Value, "-") & "_" & ReplaceSpecialChars(Nz(Me.Model.Value, ""), "-") & strExt

        Debug.Print filePath
        Debug.Print newFileName
        Debug.Print SharePointURL
 
        Set ado = CreateObject("ADODB.Stream")
        With ado
            .Type = 1 'binary
            .Open
            .LoadFromFile filePath
            .Position = 0
        End With

        Dim client As Object
        Set client = CreateObject("MSXML2.XMLHTTP.6.0")
        With client
            .Open "POST", SharePointURL & newFileName, False
            .send ado.read
            ado.Close
            
            Debug.Print .responseText
            If .Status = 200 Then '200 = OK
                MsgBox "Upload completed successfully"
            Else
                MsgBox .Status & ": " & .StatusText
            End If
        End With
    Else
        MsgBox "Image update Cancel!"
    End If
End Sub
3
  • 1
    Check if this will help. If so, please upvote. Also, do note the link to the analysis. Commented Feb 2, 2024 at 13:24
  • 1
    If you sync the folder to your local PC you can save the images to the synced folder and they'll get replicated to the SharePoint folder... Commented Feb 2, 2024 at 16:23
  • The sync method using User PC profile seems to works. My question is now, how do I grant access to users in order to be able to read/write. Also How do I handle cases where the file can be overridden? Commented Feb 3, 2024 at 20:45

0

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.