Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. Option Explicit
  2.  
  3. Public Sub WriteOutShipInspectionTable()
  4. Dim http As Object, s As String, ws As Worksheet, re As Object
  5.  
  6. Set http = CreateObject("MSXML2.XMLHTTP")
  7. Set ws = ThisWorkbook.Worksheets("Sheet1")
  8. Set re = CreateObject("VBScript.RegExp")
  9.  
  10. Dim html As HTMLDocument, body As String, headers(), startDate As String, endDate As String
  11.  
  12. startDate = "01.08.2018"
  13. endDate = "31.08.2019"
  14. headers = Array("IMO Number", "Ship Name", "Flag state", "Ship Type", "Date of inspection", "Place of inspection", "AdditCol1", "AdditCol2")
  15. Set html = New MSHTML.HTMLDocument
  16.  
  17. With re
  18. .Global = True
  19. .MultiLine = True
  20. End With
  21.  
  22. With http
  23. .Open "POST", "http://www.medmouic.org/Home/Trouver", False
  24. .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  25. .send "imonumber=&val=0&Name=&selectFlag=333&selectType=&date1=" & startDate & "&date2=" & endDate
  26. s = .responseText
  27. html.body.innerHTML = GetString(re, s, "(<table[\s\S]*?<\/table>)")
  28.  
  29. Dim totalInspections As Long, results(), r As Long, offset As Long
  30.  
  31. totalInspections = CLng(GetString(re, s, "'anyDiv', '(\d+)'"))
  32.  
  33. ReDim results(1 To totalInspections, 1 To UBound(headers) + 1)
  34.  
  35. results = PopulateArray(http, html, r, results)
  36.  
  37. For offset = 10 To totalInspections Step 10
  38. .Open "POST", "http://www.medmouic.org/Home/Trouver", False
  39. .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
  40. .send "imonumber=&val=" & CStr(offset) & "&Name=&selectFlag=333&selectType=&date1=" & startDate & "&date2=" & endDate
  41. s = .responseText
  42. html.body.innerHTML = GetString(re, s, "(<table[\s\S]*?<\/table>)")
  43. results = PopulateArray(http, html, r, results)
  44. Next
  45. End With
  46.  
  47. With ws
  48. .Cells(1, 1).Resize(1, UBound(headers) + 1) = headers
  49. .Cells(2, 1).Resize(UBound(results, 1), UBound(results, 2)) = results
  50. End With
  51. End Sub
  52.  
  53. Public Function GetAdditionalColumns(ByVal http, ByVal url As String) As Variant
  54. Dim results(0 To 1)
  55. With http
  56. .Open
  57. .GET , url, "false"
  58. 'do something with response regex if required or read into htmlDocument
  59. 'extract two items of interest result0, result1
  60. results(0) = result0
  61. results(1) = result1
  62. End With
  63. GetAdditionalColumns = results
  64. End Function
  65.  
  66. Public Function PopulateArray(ByVal http As Object, ByVal html As MSHTML.HTMLDocument, ByRef r As Long, ByRef results As Variant) As Variant
  67. Dim c As Long, tr As MSHTML.HTMLTableRow, td As MSHTML.HTMLTableCell, i As Long
  68.  
  69. For i = 1 To html.querySelectorAll("tr").Length - 1
  70. r = r + 1: c = 1
  71.  
  72. For Each td In html.querySelectorAll("tr").Item(i).getElementsByTagName("td")
  73. Select Case c
  74. Case hrefColumnNumber 'the href column
  75. Dim href As String, additionalColumns()
  76. href = html.querySelectorAll("tr").Item(i).getElementsByTagName("td").href
  77. additionalColumns = GetAdditionalColumns(http, href)
  78. results(r, 7) = additionalColumns(0)
  79. results(r, 8) = additionalColumns(1)
  80. Case Else
  81. results(r, c) = td.innerText
  82. End Select
  83. c = c + 1
  84. Next
  85. Next
  86. PopulateArray = results
  87. End Function
  88.  
  89. Public Function GetString(ByVal re As Object, ByVal s As String, ByVal p As String) As String
  90. With re
  91. .Pattern = p
  92. GetString = .Execute(s)(0).submatches(0)
  93. End With
  94. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement