Advertisement
Guest User

VBA Extract HTML Table

a guest
Jan 21st, 2022
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. Sub demo()
  2.  
  3.  
  4. Dim oApp As Outlook.Application
  5. Dim oMapi As Outlook.MAPIFolder
  6. Dim oMail As Outlook.MailItem
  7. Dim oHTML As MSHTML.HTMLDocument
  8. Dim oElColl As MSHTML.IHTMLElementCollection
  9. Dim destCell As Range
  10. Dim x As Long, y As Long
  11.  
  12. On Error Resume Next
  13. Set oApp = GetObject(, "OUTLOOK.APPLICATION")
  14. If (oApp Is Nothing) Then Set oApp = CreateObject("OUTLOOK.APPLICATION")
  15. On Error GoTo 0
  16.  
  17. Set oMapi = oApp.GetNamespace("MAPI").Folders("folder1").Folders("folder2").Folders("folder3").Folders("folder4")
  18. Set oMail = oMapi.Items(oMapi.Items.Count)
  19.  
  20.  
  21.  
  22. For Each oMail In oMapi.Items
  23. Set oHTML = New MSHTML.HTMLDocument
  24. With oHTML
  25. .Body.innerHTML = oMail.HTMLBody
  26. Set oElColl = .getElementsByTagName("table")
  27. End With
  28.  
  29.  
  30. For Each table In oElColl
  31. For x = 0 To oElColl(0).Rows.Length - 1
  32. For y = 0 To oElColl(0).Rows(x).Cells.Length - 1
  33. If y = 1 Then
  34. ActiveCell.Offset(y, x).Value = oElColl(0).Rows(x).Cells(y).innerText
  35. End If
  36. Next y
  37. Next x
  38.  
  39. Next
  40. Next
  41.  
  42.  
  43. Set oApp = Nothing
  44. Set oMapi = Nothing
  45. Set oMail = Nothing
  46. Set oHTML = Nothing
  47. Set oElColl = Nothing
  48. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement