ServerXMLHttp returns unauthorised in Excel VBA

Anonymous
2021-10-21T08:19:21.147+00:00

Trying to post data from Excel via VBA to a Google form and ServerXMLHTTP60 StatusText is returning unauthorised. Out of my depth here and coould use some help. The excel file is a local file. I checked the Form_URL value and copied to my browser manually and the data was posted as expected so I am assuming this is an access issue, but no idea where to go next. Thanks in advance

Sub SendToGoogle()
    Dim URL_First As String        'Assign the first part of URL to send the data
    Dim URL_Last As String         'Assign the last part of URL where we will update the information
    Dim Form_URL As String         'To store the Form URL after merging Beginning and End URL
    Dim HeaderName As String       'Variable to store the header type i.e. Content-Type
    Dim SendID As String           'To store the information requried to send a particular information to Google form
    Dim SRDData As MSXML2.ServerXMLHTTP60 'XML variable to send the information to server

    HeaderName = "Content-Type" 'Google recognizes this header type
    SendID = "application/x-www-form-urlencoded; charset=utf-8" 'it's required to send a particular information to Google Form

    'formResponse is used to get the response from Google Form after submitting the details
    URL_First = "https://docs.google.com/forms/d/e/MYDOC/formResponse?ifq" 'MYDOC is set to actual google form id in my code

    URL_Last = ""
    URL_Last = URL_Last & "&entry.485020870=" & 1 ' dummy data
    URL_Last = URL_Last & "&entry.107003438=" & 2 ' dummy data
    URL_Last = URL_Last & "&entry.613947283=" & 3 ' dummy data
    URL_Last = URL_Last & "&entry.1934022791=" & 4 ' dummy data
    URL_Last = URL_Last & "&submit=Submit" 'Submit - it is a command to submit the filled form

    Form_URL = URL_First & URL_Last

    Set SRDData = New ServerXMLHTTP60 'Setting the reference of new server xmlhttp 60

    SRDData.Open "POST", Form_URL, False ' Posting the entire link

    SRDData.setRequestHeader HeaderName, SendID 'Specifies the name of an HTTP header.

    SRDData.send 'Send all the information over google

    'StatusText is provide the status of data submission. It will show Ok if data will be successfully submitted
    If SRDData.statusText <> "OK" Then 'Check for successful send
      MsgBox "Please check your internet connection & required details"
    End If

End Sub
Developer technologies | Visual Basic for Applications
0 comments No comments
{count} votes

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.