Home » excel » Copying Tables from Outlook Email to Excel File – VBA

Copying Tables from Outlook Email to Excel File – VBA

Posted by: admin April 23, 2020 Leave a comment


I am currently working on a Outlook 2010 VBA macro to pull information from a email messages and place it into an Excel file. The idea is that each email has the same fields in tables embedded in the email message every time (name, order number, date, etc.) and that data is put into a spreadsheet. To do this, I have currently used the following code to get the table and move it into Excel:

'This code is inside a for each loop for each message
Set excelWorksheet2 = excelWorkbook.Worksheets.item(2)
Set excelWorksheet3 = excelWorkbook.Worksheets.item(3)
Set excelWorksheet4 = excelWorkbook.Worksheets.Add(After:=excelWorkbook.Sheets(excelWorkbook.Sheets.count))

Dim mailObj As Outlook.MailItem
Dim doc As Word.Document
Set doc = mailObj.GetInspector.WordEditor

Dim table1, table2, table3 As Object
Set table3 = doc.Tables(4).Range
Set table2 = doc.Tables(3).Range
Set table1 = doc.Tables(2).Range

Set table1 = Nothing
Set table2 = Nothing
Set table3 = Nothing

'I do much more of this to get the data from each table and put it into a master worksheet...
excelWorksheet.Cells(rows, cols + 1).Value = excelWorksheet2.Cells(4, 2).Value 'Contract Number
excelWorksheet.Cells(rows, cols + 2).Value = excelWorksheet2.Cells(4, 4).Value 'Contractor Name 

Set doc = Nothing
Set excelWorksheet2 = Nothing
Set excelWorksheet3 = Nothing
Set excelWorksheet4 = Nothing

I get the following errors every so often, but it doesn’t occur on the same data, it is sort of random and seems to occur on the Outlook/email side only:

  • “The requested member of the collection does not exist.” (Error code
    5941) at the .Range line
  • “Method ‘Copy’ of object ‘Range’ failed” at the .Copy

Sometimes both of these errors occur if I step through, if the copy fails, the macro will crash.

I have tried:

  • Putting in 2 second delays
  • Go through fewer emails (this code usually fails when I select > 10
    emails to process)
  • Clearing the clipboard after every email
  • Close/deallocate objects through Nothing (not sure if
    this is the best practice as I’m more of a C/C++/C#/Java guy)

None of these seemed to remotely fix this issue as both errors pop up frequently, but intermittently.
I’m truly at a loss as to what the next step would be in debugging this issue, any help would be much appreciated!

How to&Answers:

Based on research I have been doing on the issue of the WordEditor tables, it seems as the amount of copy/paste operations and the searching HTML tables just simply do not allow for reliable behavior. One possible solution to this could be to copy the entire email into Excel and parse the tables that way (this still requires copy/paste).

What I did to solve this problem (indirectly) is to save all the emails as HTML files (through Outlook.Application running in Excel: email.SaveAs folderPath + fileName + ".html", olHTML) in a temporary directory and have Excel open the HTML files in a workbook and work with the data that way. This has been much more reliable (added overhead though) and allows for large volumes of emails to be exported to Excel properly.

If anyone wants the exact code to my problem, message me (vindansam at hotmail.com, it’s a tad long and has some proprietary information in it).

It’s too bad that the WordEditor seems to not handle the information well, maybe Microsoft will beef things up in the next version of Office.