XML Parsing Function


May 28, 2008 – 1:58 am by JP

In keeping with my Website XML parsing post, here is a short function that returns the body text from a webpage as a string, on which you can use other functions like Instr, Left$, Mid$, etc, to extract necessary data.

VBA:
  1. Function URLText(sURL As String) As String
  2. ' check website.com using xml
  3. ' early bound
  4.  
  5. Dim xSite As XMLHTTP60
  6.  
  7. Set xSite = New XMLHTTP60
  8. xSite.Open "GET", sURL, False
  9. xSite.Send
  10.  
  11. Do Until xSite.readyState = 4
  12. Loop
  13.  
  14. URLText = xSite.responseText
  15.  
  16. End Function

And it's as simple as that. You could even use it as a UDF. Here's a sample sub:

VBA:
  1. Sub CheckMySite()
  2. Dim MyString As String
  3. Dim i As Long
  4.  
  5. MyString = URLText("http://www.google.com")
  6.  
  7. If Instr(MyString, "Hello!")> 0 Then
  8.     Cells(5,1).Value = Mid$(MyString, 1, 5)
  9. End If
  10.  
  11. End Sub

This sub checks if the returned string contains the word "Hello" and if so, pulls the first five characters of the text and puts it in cell A5. It's just an arbitrary routine to show you what you can do with the XML response.

For my next trick, I will be showing you a routine that takes a snapshot of your Outlook inbox and writes it to a spreadsheet. I'm also planning on demonstrating some code that exports Contacts from Outlook to Excel.

Enjoy,
JP


If you enjoyed this page:
  • StumbleUpon
  • Technorati
  • Digg
  • Google
  • del.icio.us
  • MisterWong

Print This Post Print This Post  |  Email This Post Email This Post  |  Permalink  |  Subscribe to this feed Subscribe now!

Filed Under: Excel, Internet Explorer, VBA, automation
Tags: , , , ,

This post has 273 views since May 28, 2008 – 1:58 am.
  1. One Response to “XML Parsing Function”

  2. Looks like I forgot to mention you will need to set a reference to Microsoft XML, in my case (Excel 2003) it is version 6.0. The filename in Windows XP is c:\windows\system32\msxml6.dll.

    --JP

    By JP on May 30, 2008

Post a Comment

To post VBA code in your comment, use [VBA] tags, like this: [VBA]Code goes here[/VBA].





Subscribe without commenting

Keep Reading:

Browse Posts:


« Save Incoming Attachments, Choose Your Folder || Open Any Email Attachment From Outlook »