I am trying to extract data from a website and parse it using VBA. The code is working absolutely fine when stepping through the code however, when running it gives an 'Object Required' run-time error. Here is the code:
Function AuthToken()
Dim XMLHTTP
Dim result As String
Dim csrf As String
Dim token As Object
Dim objHTML As Object
Set objHTML = CreateObject("htmlfile")
Set XMLHTTP = CreateObject("MSXML2.XMLHTTP.6.0")
With XMLHTTP
.Open "GET", "some website", False
.setRequestHeader "Cookie", cf
.SEND
result = .responseText
End With
With objHTML
.Open
.Write result
.Close
Set token = .getElementsByName("csrf-token")
End With
AuthToken = token(0).Content
AuthToken = Replace(Replace(Replace(AuthToken, "+", "%2B"), "/", "%2F"), "=", "%3D")
Set XMLHTTP = Nothing
End Function
The error is triggered by this line in the code:
AuthToken = token(0).Content
If I just click debug and resume, it works fine so I am really struggling to debug this. Can anyone help?