This code from http://www.mrexcel.com/forum/excel-...ge-excel-using-visual-basic-applications.html works to extract a table from an Outlook message into Excel, but only for the latest message received.
How could the code be changed so that it extracts all tables from all emails in a selected folder? (There is only one table in each email, and each table has the same number of rows and columns, if that matters).
Many thanks
How could the code be changed so that it extracts all tables from all emails in a selected folder? (There is only one table in each email, and each table has the same number of rows and columns, if that matters).
Many thanks
Code:
Option Explicit
Sub impOutlookTable()
' point to the desired email
Const strMail As String = "yourEmailAddress@MrExcelForum.com"
Dim oApp As Outlook.Application
Dim oMapi As Outlook.MAPIFolder
Dim oMail As Outlook.MailItem
On Error Resume Next
Set oApp = GetObject(, "OUTLOOK.APPLICATION")
If (oApp Is Nothing) Then Set oApp = CreateObject("OUTLOOK.APPLICATION")
On Error GoTo 0
Set oMapi = oApp.GetNamespace("MAPI").Folders(strMail).Folders("inbox")
Set oMail = oMapi.Items(oMapi.Items.Count)
' get html table from email object
Dim oHTML As MSHTML.HTMLDocument: Set oHTML = New MSHTML.HTMLDocument
Dim oElColl As MSHTML.IHTMLElementCollection
With oHTML
.Body.innerHTML = oMail.HTMLBody
Set oElColl = .getElementsByTagName("table")
End With
'import in Excel
Dim x As Long, y As Long
For x = 0 To oElColl(0).Rows.Length - 1
For y = 0 To oElColl(0).Rows(x).Cells.Length - 1
Range("A1").Offset(x, y).Value = oElColl(0).Rows(x).Cells(y).innerText
Next y
Next x
Set oApp = Nothing
Set oMapi = Nothing
Set oMail = Nothing
Set oHTML = Nothing
Set oElColl = Nothing
End Sub