Interacting with webpages from within VBA is made simple using Microsoft Internet Controls and the Microsoft HTML Object Library, (ieframe.dll, MSHTML.TLB). To use these libraries you need to add references in your excel macro to these two libraries tools -> references.
Once you have created your browser instance and navigated to a webpage you can then navigate the HTML DOM as you similarly would in other programming languages, eg
Set iHTMLCollection = HTMLDoc.all.tags("div"),
Set iHTMLCollection1 = iHTMLCollection.Item(8).Children.Item(0).Children.Item(1).Children,
If iHTMLCollection1.Item(0).getAttribute("outerHTML") > 0 Then,
If iHTMLCollection1.Item(1).outerHTML Like "*the moggy cat jumped*" The

There is already enough information out there on by Microsoft and via google to add any more VBA basics here, read on for a couple of vba functions related to browsing and the HTML DOM in VBA.


  1. Set IE = New SHDocVw.InternetExplorer
  2. IE.Visible = True
  3. IE.Navigate ("http://www.openreach.co.uk/orpg/customerzone/loadMyDashboard.do")
  4. Do While IE.Busy = True
  5. 'Animate Progress bar
  6. Loop
  7. Do While IE.ReadyState <> 4
  8. Loop
  9.  
  10. Set HTMLDoc = IE.Document
  11. Do While HTMLDoc.ReadyState <> "complete"
  12. Loop

You will need a degree of page verification and also handling server errors, you could account for all possibilities but more likely the individual project will throw up a particular repeat issue to cater for, a 500 Internal Server Error page from an overworked server for example.
In this case it could be as simple as refreshing the page IE.Refresh and carrying on with parsing the HTML DOM.


  1. 'If we get "500 Internal Server Error" refresh the page and get document again
  2. If HTMLDoc.Title Like "500 Internal Server Error" Then
  3. IE.Refresh
  4. Application.Wait (Now() + TimeValue("00:00:" & 3))
  5. Do While IE.Busy = True
  6. Loop
  7. Do While IE.ReadyState <> 4
  8. 'Debug.Print ("'500 Internal Server Error' error fix")
  9. Application.Wait (Now() + TimeValue("00:00:" & 2))
  10. Loop
  11. End If

If you intend on interacting regularly with webpages throughout your code a DOM navigation function is nescceary.



  1. Public Function NavigateHTML(ByVal tag As String, ByVal attr As String,
  2. ByVal ID As String, ByVal Stext As String)
  3.  
  4. '########################################
  5. 'tag - Find elements by TagName: 'input' OR 'a' OR 'select... ect
  6. 'attr - Search for a particular Element attribute:
  7. ' name, href, type, className ...
  8. 'ID - Specify an identifier to look for in order to confirm element and
  9. ' carry out action.
  10. ' For elements which are of type;
  11. ' text, password :Enter text into element
  12. ' submit, button, reset, image :Click element
  13. ' href="*ID*" :Click on Hyperlink element
  14. ' checkbox, radio :Select element
  15. 'Stext - If looking for text/input box specify String to enter,
  16. ' or select a certain option.
  17. '
  18. 'Example usage:
  19. '
  20. 'Call NavigateHTML("a", "href", "accounts", "")
  21. '------
  22. '
  23. 'myUsername = ping
  24. 'myPass = pong
  25. 'Call NavigateHTML("input", "name", "Email", myUsername)
  26. 'Call NavigateHTML("input", "name", "Passwd", myPass)
  27. 'Call NavigateHTML("input", "value", "Sign in", "")
  28. '------
  29. '
  30. 'Call NavigateHTML("input", "src", "/includes/confirm.gif", "")
  31. '------
  32. '
  33. 'Call NavigateHTML("select", "name", "carManuf", "Skoda")
  34. '------
  35. '
  36. 'Call NavigateHTML("input", "className", "actionBtn", "")
  37. '------
  38. '##########
  39.  
  40. Do While IE.Busy = True
  41. 'Animate Progress bar
  42. Loop
  43. Do While IE.ReadyState <> 4
  44. 'Animate Progress bar
  45. Loop
  46.  
  47. Set HTMLDoc = IE.Document
  48. Do While HTMLDoc.ReadyState <> "complete"
  49. Loop
  50. Set iHTMLCol = Nothing
  51. While iHTMLCol Is Nothing
  52. Set iHTMLCol = HTMLDoc.all.tags(tag)
  53. Wend
  54.  
  55. Select Case tag
  56.  
  57. ' Click on hyperlink
  58. Case "a"
  59.  
  60. For Each iHTMLEle In iHTMLCol
  61. If iHTMLEle.getAttribute("outerHTML") <> "" Then
  62. If iHTMLEle.outerHTML Like "*" & ID & "*" Then
  63. iHTMLEle.Click
  64. Stext = "True"
  65. Exit For
  66. Else
  67. Stext = "Could not find Attribute " & tag _
  68. & " for element type" & Stext
  69. End If
  70. End If
  71. Next
  72.  
  73. ' Select option
  74. Case "select"
  75.  
  76. For Each iHTMLSel In iHTMLCol
  77.  
  78. If iHTMLSel.getAttribute(attr) <> "" Then
  79. aStr = iHTMLSel.getAttribute(attr)
  80. If aStr = ID Then
  81. For Each iHTMLOpt In iHTMLSel.Children
  82. If iHTMLOpt.getAttribute("value") = Stext Then
  83. iHTMLSel.selectedIndex = iHTMLOpt.Index
  84. End If
  85. Next
  86.  
  87. Else
  88. 'Stext = "Could not find Attribute " & attr _
  89. & " for element type" & ID
  90. End If
  91. End If
  92. Next
  93.  
  94. ' Various
  95. Case "input"
  96. For Each iHTMLEle In iHTMLCol
  97. If iHTMLEle.getAttribute(attr) <> "" Then
  98. aStr = iHTMLEle.getAttribute(attr)
  99. If aStr = ID Then
  100. Select Case ID
  101.  
  102. ' Click button
  103. Case "submit", "button", "reset", "Submit", "Search"
  104. iHTMLEle.Click
  105. Stext = "True"
  106. Exit For
  107. ' Enter text into input box
  108. Case "text", "password"
  109. iHTMLEle.Value = Stext
  110. Stext = "True"
  111. Exit For
  112.  
  113. ' Use Stext to identify the correct checkbox/radio if multiple
  114. Case "checkbox", "radio"
  115. If Stext = iHTMLEle.getAttribute("name") Or Stext = "" Then
  116. If iHTMLEle.Checked = False Then
  117. iHTMLEle.Checked = True
  118. Else
  119. iHTMLEle.Checked = False
  120. End If
  121. Exit For
  122. Else
  123. End If
  124. Case "image"
  125. Stext = "No action setup for Tag Image"
  126. Exit For
  127. Case Else
  128. If Stext <> "" Then
  129. 'Custom form entry
  130. iHTMLEle.Value = Stext
  131. Stext = "True"
  132. ElseIf Stext = "" Then
  133. 'Custom button click, click the button
  134. iHTMLEle.Click
  135. Stext = "True"
  136. Exit For
  137. Else
  138. 'We should not be here, unkown event
  139. Stext = "Unkown Tag: " & tag & ", Attr: " & attr & _
  140. ", ID: " & ID & ", Text: " & Stext
  141. End If
  142. Exit For
  143.  
  144. End Select
  145. Else
  146. 'Stext = "Could not find Attr: " & attr & " for element: " & ID
  147. End If
  148. End If
  149. Next
  150. End Select
  151. Do While IE.Busy = True
  152. 'Animate Progress bar
  153. Loop
  154. NavigateHTML = Stext
  155.  
  156. End Function

And of course the same goes if your logging into multiple sites, this LogIn function should be called from a LoggedIn function that first checks if you are actually logged into a site, if not this LogIn function should then be called and then the LoggedIn function called again to confirm you are now logged into the site.

           
  1. Public Function LogIn(ByVal site As String)
  2. Select Case site
  3.  
  4. ' Log into google
  5. Case "google"
  6.  
  7. 'Navigate IE to site
  8. Call NavigateTo("https://accounts.google.com")
  9. 'Login with UN & PASS
  10. Call NavigateHTML("input", "name", "Email", "myUsername")
  11. Call NavigateHTML("input", "name", "Passwd", "myPass")
  12. Call NavigateHTML("input", "value", "Sign in", "")
  13.  
  14. ' Log into facebook
  15. Case "facebook"
  16.  
  17. 'Navigate IE to site
  18. Call NavigateTo("https://www.facebook.com/")
  19. 'Login with UN & PASS
  20. Call NavigateHTML("input", "name", "email", "myUsername")
  21. Call NavigateHTML("input", "name", "password", "myPass")
  22. Call NavigateHTML("input", "value", "Log in", "")
  23.  
  24. ' Log into example site
  25. Case "example"
  26.  
  27. 'Navigate IE to site
  28. Call NavigateTo("http://www.example.org/")
  29. 'Login with UN & PASS
  30. Call NavigateHTML("input", "name", "USER", "myUsername")
  31. Call NavigateHTML("Input", "name", "PASSWORD", "myPass")
  32. Call NavigateHTML("Input", "value", "LogIn", "")
  33. 'Accept Computer Misuse Act
  34. Call NavigateHTML("input", "src", "/confirm.gif", "")
  35. 'Accept terms and conditions of website use
  36. Call NavigateHTML("input", "type", "checkbox", "accepttandc")
  37. Call NavigateHTML("input", "value", "Submit", "")
  38. 'Select portal hyperlink
  39. Call NavigateHTML("a", "href", "Notice portal", "")
  40. 'Enter pin and click gif input.
  41. Call NavigateHTML("input", "name", "pin", "1234")
  42. Call NavigateHTML("input", "src", "/content/skins/roll.gif", "")
  43.  
  44. End Select
  45. Do While IE.Busy = True
  46. 'Animate Progress bar DoEvents()
  47. Loop
  48.  
  49. LogIn = "True"
  50. End Function
  51.  
  52.  
  53.  
  54.  
  55. Public Function NavigateTo(ByVal site As String)
  56. With IE
  57. .Navigate (site)
  58. Do While .Busy = True
  59. 'Animate Progress bar
  60. Loop
  61. End With
  62.  
  63. Set HTMLDoc = IE.Document
  64. Do While HTMLDoc.ReadyState <> "complete"
  65. Loop
  66. Set iHTMLCol = Nothing
  67. While iHTMLCol Is Nothing
  68. Set iHTMLCol = HTMLDoc.all.tags("input")
  69. Wend
  70. End Function

                     
  1. Function CircuitIDIs(ByVal CircuitID As String)
  2.  
  3. Dim ValidateItRtn As String
  4. ValidateItRtn = "NoMatch"
  5.  
  6. Dim Regex As Object
  7. Set Regex = CreateObject("vbscript.regexp")
  8. Regex.IgnoreCase = True
  9. Regex.Global = True
  10.  
  11. Regex.Pattern = "^([0]\d{9,10})$"
  12. If (Regex.test(CircuitID)) Then ValidateItRtn = "DN"
  13. Regex.Pattern = "^\D{4}\d{7}$"
  14. If (Regex.test(CircuitID)) Then ValidateItRtn = "SMPFID"
  15. Regex.Pattern = "^(CBUK\d{6}|CBUK\d{8})$"
  16. If (Regex.test(CircuitID)) Then ValidateItRtn = "CBUK"
  17.  
  18.  
  19. CircuitIDIs = ValidateItRtn
  20. End Function