Advertisement
qharr

Untitled

Mar 28th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. Option Explicit
  2.  
  3. 'VBE > Tools > References:
  4. ' Microsoft Internet Controls
  5. Public Sub CheckTax()
  6. Dim ie As InternetExplorer, searchBox As Object, t As Date, ws As Worksheet
  7. Const MAX_WAIT_SEC As Long = 20
  8. Dim inputValues As Range, rng As Range, i As Long
  9.  
  10. Set ie = New InternetExplorer
  11. Set ws = ThisWorkbook.Worksheets("INPUT & DATA RESULTS")
  12. Dim lastRow As Long
  13. With ws
  14. 'lastRow = .Cells(.rows.Count, "X").End(xlUp).Row '' If coming from bottom of page. Accounts for empty cells in range
  15. lastRow = .Cells(.rows.Count, "X").End(xlDown).Row ''Stops at first blank cell in range
  16. End With
  17.  
  18. Set inputValues = ws.Range("X3:X" & lastRow).Value
  19. With ie
  20. .Visible = True
  21.  
  22. For Each rng in inputValues
  23. .Navigate2 "https://vehicleenquiry.service.gov.uk/"
  24.  
  25. While .Busy Or .readyState < 4: DoEvents: Wend
  26. t = Timer
  27. Do
  28. On Error Resume Next
  29. Set searchBox = .document.querySelector("#Vrm")
  30. On Error GoTo 0
  31. If Timer - t > MAX_WAIT_SEC Then Exit Do
  32. Loop While searchBox Is Nothing
  33.  
  34. If searchBox Is Nothing Then
  35. Exit Sub
  36. Else
  37. searchBox.Focus
  38. searchBox.Value = rng.Value
  39. End If
  40.  
  41. .document.querySelector(".button").Click
  42.  
  43. While .Busy Or .readyState < 4: DoEvents: Wend
  44.  
  45. If .document.querySelectorAll("h3").Length > 0 Then
  46. 'ws.Cells(rng.Row, "Y") = "Vehicle details could not be found"
  47. 'ws.Cells(rng.Row, "Z") = "Vehicle details could not be found"
  48. Else
  49. t = Timer
  50. Do
  51. If Timer - t > MAX_WAIT_SEC Then Exit Do
  52. Loop While ie.document.querySelectorAll("#Correct_True").Length = 0
  53.  
  54. ie.document.querySelector("#Correct_True").Click
  55.  
  56. While .Busy Or .readyState < 4: DoEvents: Wend
  57.  
  58. .document.querySelector(".button").Click
  59.  
  60. While .Busy Or .readyState < 4: DoEvents: Wend
  61.  
  62. Dim items As Object, taxInfo As String, motInfo As String
  63. t = Timer
  64. Do
  65. On Error Resume Next
  66. Set items = ie.document.querySelectorAll("strong")
  67. On Error GoTo 0
  68. If Timer - t > MAX_WAIT_SEC Then Exit Do
  69. Loop While items.Length = 0
  70.  
  71. taxInfo = Replace$(items.item(0).innerText, "Tax due: ", vbNullString)
  72. motInfo = Replace$(items.item(1).innerText, "Expires: ", vbNullString)
  73.  
  74. ws.Cells(rng.Row, "Y") = taxInfo
  75. ws.Cells(rng.Row, "Z") = motInfo
  76. End If
  77. Set searchBox = Nothing: Set items = Nothing
  78. Next
  79. .Quit
  80. End With
  81. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement