following code-snippet searches with google search for company-names. this code is working in excel:
Dim el As Object
Dim http As Object
Dim html As New HTMLDocument
Dim lng_row_start As Long
Dim lng_row As Long
Dim lng_row_new As Long
Dim int_column_name As Integer
Dim int_column_news As Integer
Set http = CreateObject("MSXML2.XMLHTTP")
'------------
str_sheet = "news"
lng_row_start = 3
int_column_name = 1
int_column_news = 3
'------------
lng_row = lng_row_start
lng_row_new = lng_row_start
Do While 0 < Len(Sheets(str_sheet).Cells(lng_row, int_column_name).Value)
str_google = Replace(Sheets(str_sheet).Cells(lng_row, int_column_name).Value, " ", "+")
http.Open "GET", "https://www.google.com/search?q=" & str_google & "&tbm=nws", False
http.send
html.body.innerHTML = http.responseText
'--------
Set el = html.getElementById("rso")
now i want to do that in access. using XMLHTTP gives me "no permission"-error on the "http.send"-line. using ServerXMLHTTP gives me a responseText saying that i got the "error 403. client has no permission to get url".
now i added this line for the ServerXMLHTTP:
http.setRequestHeader "User-Agent", "Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1)"
now i get an responseText saying something about "signing in/login". im german so it tells me "Anmelden".
so i still cant get the google-search result.
some ideas? maybe how i get a correct requestHeader so i dont get the "login" responseText?
i use ms-access 2007-20016.
following is my access code snippet:
On Error GoTo err_stan
'DEFINITION
Dim str_google As String
Dim el2 As New HTMLDocument
Dim el3 As New HTMLDocument
Dim el4 As New HTMLDocument
Dim el As New HTMLDocument
Dim http As Object
Dim html As New HTMLDocument
Dim db As DAO.Database
Dim rs_companies As DAO.Recordset
Dim rs_news As DAO.Recordset
'DECLARATION
Set db = CurrentDb
Set rs_companies = db.OpenRecordset("SELECT DISTINCT companyName FROM qGoogleSearch")
Set rs_news = db.OpenRecordset("SELECT * FROM Tnews")
'Set http = CreateObject("MSXML2.XMLHTTP.6.0")
Set http = CreateObject("MSXML2.ServerXMLHTTP.6.0")
'ALGORITHM
rs_companies.MoveFirst
Do While Not rs_companies.EOF
str_google = "https://www.google.com/search?q=" & _
Replace(rs_companies.Fields("companyName").Value, " ", "+") & _
"&tbm=nws"
'http.SetOption 2, 13056
http.Open "GET", str_google, False
'http.setRequestHeader "User-Agent", "Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1)"
http.send
html.body.innerHTML = http.responseText
Set el = html.getElementById("rso")
EDIT: using this url works: https://www.google.com/search?q=bango+plc
this does give a permission error: https://www.google.com/search?q=bango+plc&tbm=nws in excel it works fine...
why does XMLHTTP work in excel but not in access? i tried to put the msaccess-file in a trusted location. did not work
Update try this and report back:
This is what works for me: