Retrieving Tables from outlook to excel

Status
Not open for further replies.

Davefin

New Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
@Diane Poremsky ,
You were able to help me on another topic, Which again thanks. Now I've come up with another project I'd like to achieve. I get several emails a day (as included here for samples) that I would like to extract to excel. from there I can manipulate the data to my needs. I've tried to mix and match sample codes from various sites only to get errors or results that are unwanted. There where some close results but not what I was looking for. What I'm looking for is to grab the table data (as highlighted in the sample) and dump it in the same work book and sheet as new emails come in. I know I can assign a rule to run it as I get those emails.
I tried to upload the email but I guess its not allowed so here is a snippet of a sample email I get.
Thanks in advanced
Dave-

email1.PNG
 
Typically, you'd use regex or instr/mif functions to get the values and send them over to excel... for the table, i'm undecided if using an array would be easier or just trying to copy the table and paste it. Is the message always identical (so the code can find table 2, copy rows 2 - x)?
 
The macro that is marked as the answer at Copy a table from body of an email to Excel spreadsheet should get you close. If you need the last table, then change
For x = 1 To doc.tables.Count
to
x = doc.tables.Count

and remove the last Next

it's going to pick up the header as written, but should work good otherwise, especially as long as no fields are merged. (I'm checking on ways to skip the header row and use range)

As written, it works on selected messages - if you want to use it in a rule, it can be tweaked.
 
This code should work with a run a script rule -
Code:
Sub dd(Item As Outlook.MailItem)
Dim r As Object  'As Word.Range
Dim doc As Object 'As Word.Document
Dim iRow As Long 'row index
Dim xlApp As Object, wkb As Object
Set xlApp = CreateObject("Excel.Application")
Set wkb = xlApp.Workbooks.Add
xlApp.Visible = True

Dim wks As Object
Set wks = wkb.Sheets(1)

Set doc = Item.GetInspector.WordEditor
 '   For x = 1 To doc.Tables.Count
     x = doc.Tables.Count
     Set r = doc.Tables(x)
' get rows 2 - end:
     For iRow = 2 To r.Rows.Count
        r.Rows(iRow).Range.Copy
' to get all rows in the table
     ' r.Range.Copy
       wks.Paste
       wks.Cells(wks.Rows.Count, 1).End(3).Offset(1).Select
    Next
End Sub
 
Thanks for the reply i'll put it to the test and give you an update. Sometimes the email will contain more than one table. I think the most I've seen so far is 4 of em with the same header
 
So the link you pointed out I have seen and that one was the closest to what I wanted to achieve. The problem with that one is that
1: it creates a new workbook every time it runs
2: I really wanted to avoid copying the first table.:
upload_2017-9-6_9-18-57.png

Here is a little more info on these emails too if this helps
Usually the first email doesn't have any data in the tables because nothing has occurred.
so it looks like this:
upload_2017-9-6_9-21-59.png

as reports come in it looks like the above. The first table is always just that table the second table can go anywhere from 1 to 5 or more but the layout is always the same
 
it creates a new workbook every time it runs
yeah, that code does. meant to post this link last night - Copy data from Outlook email tables to Excel - I tweaked the macro. It gets either the last (or could get any specific table by index #) or all - changing the 'all' code to get 2 to count should get 2 - last:
For x = 2 To doc.Tables.Count

my need an 'on error resume next' if the blank table errors, or
if r.Rows.Count > 1 then
For iRow = 2 To r.Rows.Count
' paste into sheet
next
end if
 
or all - changing the 'all' code to get 2 to count should get 2 - last:
when changing to get all i get this:
upload_2017-9-6_13-32-57.png

I shrank the cells to fit on one page.
Now when I change: For x = 1 To doc.Tables.Count
to: For x = 2 To doc.Tables.Count
I get rid of the junk rows 1-12. If I change the 2 to a 3 i get rid of the unwanted 13-29 but loose the wanted table directly underneath. The rest of the tables are pasted tho. I did notice that there are some merged cells on the email. I would post a sample email but this site wont let me post a .mgs file.
I tried to change the xlsheet.paste to xlsheet.PasteSpecial xlPasteValues
but when it ran it looked as if those were pasted pictures. the formatting isn't really that important because i'll will be removing duplicates and then pasting the values to another document anyway.
 
Yeah, merged cells will be a problem. Reading the rows eliminates the merged cell problems, but i'm not sure if it will work with multiple tables. Will check.

zip the email then you should be able to upload it.
 
below is the code i have so far to get just the last 2 tables (getting all tables works, but it also includes the body text) - i'm trying to get just the table with the RS Type in the first cell but it only gets the last - the debug.print results shows why - there are 3 tables. First begins with the characters 'Greetin'. Table 2 begins with 'Repeat'. Only table 3 meeting the condition of the If statement.

1 tables
Greetin Trimmed
2 tables
Repeat Trimmed
3 tables
RS Type Trimmed

i honestly don't know how to get around it - i think its because there isn't a space between the first 2 tables, but cant be 100% sure.

This is what outlook sees as table 1 and 2:
table1-2.png
Code:
Sub RunScript()
Dim objApp As Outlook.Application
Dim objItem As MailItem
Set objApp = Application
Set objItem = Application.ActiveExplorer.Selection.Item(1)

'macro name you want to run goes here
dd objItem

End Sub


Sub dd(Item As Outlook.MailItem)
Dim r As Object  'As Word.Range
Dim doc As Object 'As Word.Document
Dim iRow As Long 'row index
Dim xlApp As excel.Application 'Object
Dim wkb As excel.Workbook ' Object
Dim wks As excel.Worksheet ' Object
Set xlApp = CreateObject("Excel.Application")
Set wkb = xlApp.Workbooks.Add
xlApp.Visible = True

Set wks = wkb.Sheets(1)

Set doc = Item.GetInspector.WordEditor
    For x = 1 To doc.Tables.Count
     Set r = doc.Tables(x)
     Debug.Print x, "tables"
' get rows 2 - end:
'For iRow = 2 To r.Rows.Count
StrCellText = r.Cell(1, 1).Range.Text
StrCellText = Replace(StrCellText, vbCr, "")
StrCellText = Replace(StrCellText, Chr(7), "")

On Error Resume Next
Debug.Print Trim(Left(StrCellText, 7)), "Trimmed"
If Trim(Left(StrCellText, 7)) = "RS Type" Then

' to get all rows in the table
      r.Range.Copy
    wks.Cells(wks.Rows.Count, 1).End(xlUp).PasteSpecial xlPasteValues
End If
   'Next iRow
   Next x
End Sub
 
upload_2017-10-31_8-47-1.png

Was I supposed to change something?
also I wanted to thank you again for your time that you have taken to look at this.
 
You need to set a reference to Excel Object library in Tools, References. if you use as object (which i commented out), you don't need to do this but some bits of the object model aren't available.
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
V vBA for searching a cell's contents in Outlook and retrieving the subject line Outlook VBA and Custom Forms 1
S Retrieving Deleted Outlook Contacts Using Outlook 2
R Outlook 2013 stalls when retrieving from Frontier POP3 Using Outlook 3
F Retrieving old BCM data BCM (Business Contact Manager) 5
J ModefiedForm page issues 1)retrieving available size of Modifiedfo Outlook VBA and Custom Forms 3
A Retrieving CalendarView for Outlook 2003 Outlook VBA and Custom Forms 4
A Retrieving work week (start/end times) from outlook with VBA Outlook VBA and Custom Forms 5
R Retrieving email item Outlook VBA and Custom Forms 5
L Retrieving the DisplayFormat for a UserProperty using VB Outlook VBA and Custom Forms 3
S Email Format With Embedded Images and Tables Change Using Outlook 2
W controlling margins in outlook emails with tables Using Outlook 1
Vijay Kumar Tables in outlook body Outlook VBA and Custom Forms 1
Vijay Kumar Copy tables from outlook to excel Using Outlook 12
L Android Outlook Doesn't Update PC Notification Changes Using Outlook 0
A How to open Excel file saved in Outlook folder? Outlook VBA and Custom Forms 4
D.Moore Outlook desktop client suggested searches question Using Outlook 13
Y Outlook 2016 (64-bit) Copy Local Cal. Events to Another Cal. with Modified Reminder time Using Outlook 2
T Outlook 2019 While connecting an IMAP account in "classic" Outlook 2024 I caused a massive duplication of emails on the server (death loop) Using Outlook 5
D Cannot logon to Outlook.com, or outlook on Mac, outlook not updating on ipad, iphone Using Outlook 1
J unable to get my new install of Outlook to display mailboxes in the single-line format. Using Outlook 1
D Legacy Microsoft Outlook for Mac Support will end in Oct 2025 Using Outlook 5
C Nasty Bug Lurking In Outlook For Years. The Trigger. Any Fix Or Workaround? Using Outlook 11
R Auto clicking Hyperlink in outlook Outlook VBA and Custom Forms 7
ughlook Open multiple contacts in NEW Outlook? Using Outlook 3
G Outlook translation feature is off Using Outlook 2
J Outlook 2010 does not let me put any account Using Outlook.com accounts in Outlook 3
P 3 of 5 PST files don't install from d:\outlook but only from D:\ Using Outlook 7
HarvMan January Windows 10 preview update force installs new Outlook Using Outlook 1
L Outlook 2010 - new installation on Windows 11 - aplzod32.dll is not a valid Add-in Using Outlook 12
J Outlook troubleshooting/logging - option grayed out Using Outlook 2
B Arrows missing from Outlook emails vertical scrollbar Using Outlook 0
G Outlook 2021 (New) doesn't respect default browser Using Outlook 9
B Outlook or iPhone turning tabs into spaces in Outlook Notes Using Outlook 1
P newly installed Office 365 includes OLD Outlook Using Outlook 6
R Outlook ribbon menu default? Using Outlook 7
H Spam email in Gmail not visible in Outlook Using Outlook 3
J How to transfer Win 10 Outlook to new Windows 11 pc? Using Outlook 16
J Renegade spam URL line displayed in old local Outlook 365 email title Using Outlook 3
G Reduce whitespace in Outlook desktop Contact Cards display Using Outlook 3
C Outlook classic via 365 Using Outlook 2
Dr. Demento Analogous Outlook code to read info into an array (or collection or whatever) Outlook VBA and Custom Forms 7
S Repair Outlook Using Outlook 8
V Outlook Form ListBox is not editable Outlook VBA and Custom Forms 2
F Outlook's contacts Using Outlook 1
D Outlook 2003 stopped dead Using Outlook 2
G Cannot receive emails from gmail account in Outlook 365 Using Outlook 1
E "Cannot display the folder. MS Outlook cannot access the specified file location" Using Outlook 8
P Outlook 2016 Working Offline Using Outlook 2
Rupert Dragwater Cannot reestablish gmail (email address) account in Outlook 365 Using Outlook 11
O Outlook 365 synchronisieren Exchange Server Administration 1

Similar threads

Back
Top