how to get bing api search results in MsOffice VBA

For the loyal VBA fans a quick snippet to grab search results from Bing in Access or Excel. There are few articles on the web about that, and that is a shame. So lets add some content on it to the web : first in MsAccess VBA and then in Excel, with a sample workbook added.

Working with the BING API requires an Application Id you can get at the Bing website.

Accessing the Bing Api in MsAccess VBA

In the the VBA Ide, through the menu Tools References I add a reference to the XML object library that contains the XMLHTTPRequest object, enabling me to make HTTP requests from VBA in the MsAccess database.

The simple part is the http request string :

  2.         Dim MyKeyword As String
  3.         MyKeyword = "seo"
  5.         Dim requestString As String
  6.         requestString = ""
  8.         Dim AppId As String
  9.         AppId = "(get one at the bing website)"
  11.        ' Common request fields (required)
  12.         requestString = requestString & _
  13.         "AppId=" & AppId & "&Query=" & MyKeyword & "&Sources=Web"
  15.         ' Common request fields (optional)
  16.         requestString = requestString & _
  17.               "&Version=2.0" _
  18.             & "&Market=en-us" _
  19.             & "&Adult=Moderate" _
  20.             & "&Options="
  22.         ' Web-specific request fields (optional)
  23.         requestString = requestString & _
  24.               "&Web.Count=10" _
  25.             & "&Web.Offset=0" _
  26.             & "&Web.Options=DisableHostCollapsing+DisableQueryAlterations"

Then comes the working part, sending the actual request and receiving the XML response (objSvrHTTP.responseText) containing the search data. I use a DomDocument object and XPath to get at the XML data.

First sending the request

  1.     objSvrHTTP.Open "GET", requestString, False
  2.     objSvrHTTP.send requestString

then creating a domdocument and receiving the response in it

  1.     'Create the DomDocument Object
  2.     Dim oDoc As MSXML2.DOMDocument
  3.     Set oDoc = CreateObject("MSXML2.DOMDocument")
  4.     oDoc.async = False
  5.     oDoc.validateOnParse = False
  7.     'Load the response in the DomDocument Object
  8.     Dim fSuccess As Boolean
  9.     fSuccess = oDoc.loadXML(objSvrHTTP.responseText)
  10.     If Not fSuccess Then
  11.         MsgBox "failed"
  12.         Exit Sub
  13.     End If

The response uses two namespaces and the data we actually want is in the “web” namespace, so before we start selecting nodes in the xml-tree we first indicate what namespace we want to access :

  1.     oDoc.SetProperty "SelectionNamespaces", "xmlns:web=''"

The total search result pages is a top level node in the namespace :

  1.     Set xmlnode = oDoc.selectSingleNode("//web:Total")
  2.     Debug.Print xmlnode.Text

…but the good stuff is in the “WebResult” nodes in the xml tree, I can use an XPath reference //web:Webresult to access that node-collection and with a simple for-next iterate through the collection, selecting the single nodes containing the actual data.

  1.     Set oChildren = oDoc.selectNodes("//web:WebResult")
  2.     For Each oResult In oChildren
  3.         Debug.Print oResult.selectSingleNode("./web:Title").Text
  4.         Debug.Print oResult.selectSingleNode("./web:Description").Text
  5.         Debug.Print oResult.selectSingleNode("./web:Url").Text
  6.         Debug.Print oResult.selectSingleNode("./web:DisplayUrl").Text
  7.         Debug.Print vbCrLf
  8.     Next

I output to the Immediate window aka Debug (under View, Immediate Window or Ctrl+G)

What can we do with it ? Let’s make a table for date, keyword, url, title, description, position

and add some code to store the results we retrieve

  1.     Dim table1 As DAO.Recordset
  2.     Set table1 = CurrentDb.OpenRecordset("Table1", dbOpenTable)
  3.     'counter for position    
  4.     i = 0
  6.     Set oChildren = oDoc.selectNodes("//web:WebResult")
  7.     For Each oStruct In oChildren
  9.        i = i + 1
  10.        With table1
  11.             .AddNew
  12.             'data from bing
  13.             !TITLE = oStruct.selectSingleNode("./web:Title").Text
  14.             !Description = oStruct.selectSingleNode("./web:Description").Text
  15.             !url = oStruct.selectSingleNode("./web:Url").Text
  16.             'and some additional data
  17.             !MYDATE = Now()
  18.             !POSITION = i
  19.             !KEYWORD = MyKeyword
  20.             'store the record
  21.             .Update
  22.         End With
  24.     Next
  26.     table1.Close

There now that scratches the seo itch :

MsAccess stuff is a bit hard to put on the web as sample, so I’ll make a quick one in Excel and put it up for download.

Accessing the Bing Api in Excel VBA

there you go : on sheet 1 you can fill in your AppID and keyword, click the butt(on…

…and on sheet 2 you get the top 10 search results

It uses the cells B1 and B2 as named ranges for MyKeyword and AppID, and has the code in the enclosed module. It also contains one of my AppId’s so no querying nasty shit, please.

wordpress xmlrpc and microsoft access vba

I was building a database in WordPress but their table interface lacks completely and I dont want to spend 200 hours developing a backend for a once-off project.

I always used MsAccess for RAD blueprinting and I reckon you can build a better more flexible interface in 20 hours. With XHR MsAccess has some better internet capabilities these days, a nice simple XMLHttp library.

You can use it for any http rest api in the cloud.

Ajax for Access :) here’s the most basic wordpress xml-rpc call, sayHello.

  1. Sub PutXML()
  3. txtURL = ""
  4. txtUserName = "user"
  5. txtPassword = "pwd"
  7.   Dim objSvrHTTP As ServerXMLHTTP
  8.   Dim strT As String
  9.   Set objSvrHTTP = New ServerXMLHTTP
  11.   objSvrHTTP.Open "POST", txtURL, False, CStr(txtUserName), _
  12.    CStr(txtPassword)
  14.   objSvrHTTP.setRequestHeader "Accept", "application/xml"
  15.   objSvrHTTP.setRequestHeader "Content-Type", "application/xml"
  17.   strT = ""
  18.   strT = strT & "<methodcall>"
  19.   strT = strT & "<methodname>demo.sayHello</methodname>"
  20.   strT = strT & "</methodcall>"
  22.   objSvrHTTP.send strT
  24.   MsgBox objSvrHTTP.responseText
  26. End Sub

For pre-Vista you need the MSXML 6.0 library from microsoft, in Vista I already had it installed so you can add a reference to the library and off you go.