Extract first table of Outlook mail folder

abhishukla15

New Member
Joined
May 12, 2015
Messages
31
I'm trying to extract first table of each mail of a specific folder to Excel. If there is more than one table in the mail we can exclude it and move to next mail item. Below is the code I have at the moment. Could you please hel

<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">PublicSub Import_Tables_From_Outlook_Emails()
Dim oApp As Outlook.Application, oMapi As Outlook.MAPIFolder
Dim oMail As Outlook.MailItem, HTMLdoc As MSHTML.HTMLDocument
Dim tables As MSHTML.IHTMLElementCollection, table As MSHTML.HTMLTable
Dim objExcelApp As Excel.Application, x AsLong, y AsLong, destCell As Range
Dim objExcelWorkbook As Excel.Workbook, objExcelWorksheet As Excel.Worksheet

Set objExcelApp = CreateObject("Excel.Application")'Create a new excel workbook
Set objExcelWorkbook = objExcelApp.Workbooks.Add
objExcelApp
.Visible =True
Set destCell = ActiveSheet.Cells(Rows.Count,"A").End(xlUp)

OnErrorResumeNext
Set oApp = GetObject(,"OUTLOOK.APPLICATION")
If oApp IsNothingThenSet oApp = CreateObject("OUTLOOK.APPLICATION")
OnErrorGoTo0

Set oMapi = oApp.GetNamespace("MAPI").PickFolder
IfNot oMapi IsNothingThen
ForEach oMail In oMapi.items
'Get HTML tables from email object
Set HTMLdoc =New MSHTML.HTMLDocument
With HTMLdoc
.Body.innerHTML = oMail.HTMLBody
Set tables =.getElementsByTagName("table")
EndWith
ForEach table In tables
For x =0To table.Rows.Length -1
For y =0To table.Rows(x).Cells.Length -1
destCell
.Offset(x, y).Value = _
table
.Rows(x).Cells(y).innerText
Next y
Next x
Sheets
.Add After:=ActiveSheet
Range
("A1").Activate
Set destCell = ActiveSheet.Range("A1")
Next
Next
EndIf
Set oApp =Nothing
Set oMapi =Nothing
Set oMail =Nothing
Set HTMLdoc =Nothing
Set tables =Nothing
MsgBox
"Finished"
EndSub</code>
 
Last edited:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top