CreateObject("MSXML2.ServerXMLHTTP.6.0") - excel

I'm currently facing some issues with creation of CreateObject("MSXML2.ServerXMLHTTP.6.0") object.
Public Function PrepareHttpRequest(Request As RestRequest, TimeoutMS As Long, _
Optional UseAsync As Boolean = False) As Object
Dim Http As Object
**Set Http = CreateObject("MSXML2.ServerXMLHTTP.6.0")**
' Set timeouts
Http.setTimeouts TimeoutMS, TimeoutMS, TimeoutMS, TimeoutMS
' Add general headers to request
Request.AddHeader "User-Agent", UserAgent
Request.AddHeader "Content-Type", Request.ContentType
If Request.IncludeContentLength Then
Request.AddHeader "Content-Length", Request.ContentLength
Else
If Request.Headers.Exists("Content-Length") Then
Request.Headers.Remove "Content-Length"
End If
End If
' Pass http to request and setup onreadystatechange
If UseAsync Then
Set Request.HttpRequest = Http
Http.onreadystatechange = Request
End If
Set PrepareHttpRequest = Http
End Function
The above code is working fine for some users but its failing in some users machine. Can anyone please provide necessary changes to fix issue.

Try:
Set objXML = Server.CreateObject("MSXML2.ServerXMLHTTP")
Otherwise put an On Error Goto ln and tell us what the Err.Description is and google it
Run ProcessMonitor to see where its trying to find the DLL and regsvr32 the msxml6.dll

Related

Excel VBA API Request

So I don't have any experience coding whatsoever but I'm working on pulling some vehicle data from an government site with an API using VBA. so i don't have to manually adjust data in our vehicle list.
the code i wrote/stole:
Sub SendAPIRequest()
Dim httpreq As Object
Dim url As String
Dim response As String
Dim headers As Collection
Set headers = New Collection
headers.Add "SVV-Authorization", "Apikey {1234}"
Set httpreq = CreateObject("MSXML2.XMLHTTP")
url = "https://www.vegvesen.no/ws/no/vegvesen/kjoretoy/felles/datautlevering/enkeltoppslag/kjoretoydata?kjennemerke=AA91620" + kjennemerke
With httpreq
.Open "GET", url, False
.setRequestHeader "SVV-Authorization", "1234}"
.send
End With
response = httpreq.responseText
Debug.Print response
End Sub
the request goes out but the with the following response: "status":403,"error":"Forbidden","path":"/enkeltoppslag/kjoretoydata"}
403 The API key does not exist in the database, has the status active and/or the user is blocked.
Additional info: REST service - Json response.
https://autosys-kjoretoy-api.atlas.vegvesen.no/api-ui/index-enkeltoppslag.html
key is legit so I'm sending the header out wrong?
Thanks in advance :)
copy & paste other code, etc.

HTTP request to sharepoint site

I'm trying to see if a file exists on sharepoint. For this is use below code. If I use "MSXML2.XMLHTTP.6.0" then I get an access denied error.
With "MSXML2.ServerXMLHTTP.6.0" the oHttpRequest.Status returns always 200 even if the file is not there. The URL gets passsed with URLStr and looks something like this:
"https://company.sharepoint.com/sites/abc/def/xyz.xlsx"
If I open the file with other VBA code the first example runs fine (maybe because authentication was done somehow?). Online many posts said to add "s" to "http" -> "https", but it changed nothing.
The idea is that the workbook checks online if it is the latest version and if not informs the user. Maybe there is a better way to do this?
Public Function checkversionmethod2(URLStr As String) As Boolean
Dim oHttpRequest As Object
'Set oHttpRequest = CreateObject("MSXML2.XMLHTTP.6.0") 'authentication issue
Set oHttpRequest = CreateObject("MSXML2.ServerXMLHTTP.6.0") 'always return 200
With oHttpRequest
.Open "HEAD", URLStr, False ', [UserName], [Password]
.setRequestHeader "Cache-Control", "no-cache"
.setRequestHeader "Pragma", "no-cache"
.send
End With
If oHttpRequest.Status = 200 Then
fileexists = 1
Else
fileexists = 3
End If
Set oHttpRequest = Nothing
End Function

POST api authentication in vba

I am trying to connect excel (through vba) to the "teamleader" api v1, via post calls.
api docs here
I tried some solutions which I have found, regarding post calls in vba, but seem to be getting nowhere.
This is what I have so far:
Sub test2()
Dim url As String
Dim objhttp As Object
Set objhttp = CreateObject("MSXML2.ServerXMLHTTP")
url = "https://app.teamleader.eu/api/helloWorld.php"
With objhttp
.Open "POST", url, False
.setRequestHeader "api_group", "xxx"
.setRequestHeader "api_secret", "xxx"
.send
Debug.Print .responseText
End With
End Sub
This however gives me this response:
{"status":"failed","reason":"Please set api_group."}
Does anyone have any idea how to get this to work?
Turns out I needed the put all the authentication in the url itself, this question can be closed.

How to call an API in visual basic?

I am trying to call an API service in visual basic, and then the idea is to use the jsonparser to pull specific data from this.
I get "empty" response here. When I use Postman it works.
Sub getdata()
Dim ws As Worksheet: Set ws = Worksheets("Datalastcall")
Dim http As Object
Set http = CreateObject("WinHttp.WinHttprequest.5.1")
Url = "https://api.teamtailor.com/v1/jobs?include=user,team-memberships.user,stages&filter%5Bstatus%5D=unlisted"
http.Open "Get", Url, False
http.SetRequestHeader "X-Api-Version", "20161108"
http.SetRequestHeader "Authorization", "Token Token=xxx"
http.Send
httpGET = http.ResponseText
Cells(1, 1) = http.ResponseText
As discussed in the comments it should say token = instead of Token =.

Error handling with XMLHTTP on link not existing

I use XMLHTTP to connect to a API of a webserver and retrieve some data.
I connect to a http url so I can access from every position, but I want to do that when I am in office, if there is no internet connection, the macro is continuing to working connecting to a second url (internal IP).
I was thinking something like that:
Dim basic_url, basic_url_web, basic_url_local As String
basic_url_web = "www.notexistingurl.com"
basic_url_local = "192.168.178.3"
Dim myurl As String
On Error Resume Next
myurl = basic_url_web + "/api/product/read_product.php?id=4"
xmlhttp.Open "GET", myurl, False
xmlhttp.Send
If Err.Number <> 0 then
myurl = basic_url_local + "/api/product/read_product.php?id=4"
xmlhttp.Open "GET", myurl, False
xmlhttp.Send
END IF
On Error GoTo 0 'error checkin restored
But it is not working, actually when I call the xmlhttp.Send function I ever have a -2147467259 error number, and this is the same error number I have if actually there is no error.
If I don't use the "On Error Resume Next", then I have automation error on the .send() function (on an inexistent link), and therefore I cannot work with it.
I'm becoming crazy, I took about only 2-3 hours to do all the connection with the API and everything was working perfectly, but now I'm working since days to find a solution to have a second URL if the first cannot be reached.
Thank you for your suggestions

Resources