Copy Outlook Email HTML Body as Table to Excel

E1375

New Member
Joined
Jun 12, 2023
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hello !!!

I found this Sub here in MrExcel....

Sub OLook_to_Excel()
Dim oApp As Outlook.Application, oMapi As Outlook.MAPIFolder
Dim oMail As Outlook.MailItem
Dim oHTML As MSHTML.HTMLDocument: Set oHTML = New MSHTML.HTMLDocument
Dim oElColl As MSHTML.IHTMLElementCollection, x&, y&

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(1).Folders(1) ' desired folder
Set oMail = oMapi.Items(oMapi.Items.Count) ' desired email

With oHTML
.Body.innerHTML = oMail.htmlBody
Set oElColl = .getElementsByTagName("table")
End With

For x = 0 To oElColl(0).Rows.Length - 1 ' import
For y = 0 To oElColl(0).Rows(x).Cells.Length - 1
[A1].Offset(x, y) = oElColl(0).Rows(x).Cells(y).innerText
Next
Next
Set oApp = Nothing: Set oMapi = Nothing
Set oMail = Nothing: Set oHTML = Nothing
Set oElColl = Nothing
End Sub

Trying to use it.... I am getting error "Array index is out of bounds" when execution reaches:

Set oMail = oMapi.Items(oMapi.Items.Count) ' desired email

The value of oMail.Items.Count is zero. I do have unread emails in my inbox. In fact the email I am trying to process is the latest received and unread.

Could you please provide your valuable advise?

Thank you very much

e1375
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
After the line that gets the desired folder, temporarily add the following line to see whether or not it in fact is pointing to the desired folder...

VBA Code:
MsgBox oApp.GetNamespace("MAPI").Folders(1).Folders(1).Name

Does it point to the desired folder?
 
Upvote 0
Thank you for your prompt response. The answer is no. It is pointing to the "Deleted Items" folder instead of the "Inbox" folder. I understand now why the error is being caused. How to make it point to the "Inbox" folder?

Thank you

e1375
 
Upvote 0
Is it your default folder? If so, try the following instead...

VBA Code:
Set oMapi = oApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)

Hope this helps!
 
Upvote 0
Thank you. It does, the error is gone. Now the Sub is actually reading the desired eMail. There is a new problem with the remaining code of this sub since it does copy the entire eMail body into Sheet1 cell A1, instead of copying the Excel table contained in the read email to Sheet1. What I am trying to do is copy a table from an incoming email to a worksheet. Do you know if there is a piece of code that does this? I have read that the body of an email can be read with VBA as a String, as HTML and as RTF. What I am trying to do is retrieve the table from the eMail to Sheet1, with its data but also all the formatting. Please advise.

e1375
 
Upvote 0
First set the following reference (Visual Basic Editor >> Tools >> References) . . .

VBA Code:
Microsoft Forms Object Library...

Then try something like this...

VBA Code:
    'Your code here'  
    '
    '
   
    Dim oDataObject As MSForms.DataObject
    Set oDataObject = New MSForms.DataObject
   
    oDataObject.SetText oElColl(0).outerHTML
    oDataObject.PutInClipboard
   
    ActiveSheet.Paste Range("A1")

Hope this helps!
 
Upvote 1
Solution
Hi !!!

I just started doing testing. It seems like your code above can do what I need. I need to do more testing. Will come back today or tomorrow at the latest, to provide more information.

In the meantime, thank you.

e1375
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,173
Members
452,615
Latest member
bogeys2birdies

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