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
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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)?
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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
 

Davefin

New Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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
 

Davefin

New Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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
 

Davefin

New Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
no, sorry - i started looking at it and ran into problems and set it aside. will take another look.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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
 

Davefin

New Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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
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
C retrieving deleted calendar Using Outlook 1
J retrieving a Word file opened in Outlook Using Outlook 3
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
M Tables lost in Sent Email and Can't see Table in Viewed Inbox email. Using Outlook 2
M Tables lost in Outlook Sent Messages Using Outlook 2
P Tables in RTF Signature Using Outlook 3
witzker print-list-of-outlook-folders with sort posibility Outlook VBA and Custom Forms 0
witzker Open Contact missing in Outlook 2019 Using Outlook 2
D.Moore VB script to Digitaly Sign newly created outlook message Outlook VBA and Custom Forms 2
B Outlook 2016 Retail C2R keeps logging since update? Using Outlook 0
M outlook 365 trying to finish my sentences Using Outlook 0
D Outlook Address Book Using Outlook 0
B Emails get stuck in Outbox of Gmail IMAP in Outlook for Windows Using Outlook 0
C Not sync folders not found after MS Outlook 365 update Using Outlook 1
V Outlook 2016 Does Outlook-2016 (64 bit) work with iCloud for Windows ? Using Outlook 4
R Microsoft Outlook 2016 - Gmail not sending, asks for password for SMTP, tried different ports Using Outlook 23
V Outlook 2016 Outlook-2016 and iCloud for Windows - Problems Using Outlook 2
O VBA Outlook Message Attachment - Array Index Out of Bounds Outlook VBA and Custom Forms 0
K Imap PC Outlook + Android phone Using Outlook 1
GregS Outlook 2016 Move Outlook to new computer? Using Outlook 4
I Outlook 2003 shows html code when To: field is empty Using Outlook 7
O The Outlook API wrongfully shows an outlook folder to have zero sub-folders Outlook VBA and Custom Forms 1
O The Outlook API wrongfully shows an outlook folder to have zero sub-folders Outlook VBA and Custom Forms 2
C All Gmails don't show in Outlook 2019 Using Outlook 3
D Outlook 2007 vs. Outlook 2010 -- ToDo Bar Using Outlook 0
U Outlook 2016 Outlook 2016 sender name Using Outlook 1
T Compuserve, Yahoo, Oath2 and Outlook 2013 Using Outlook 4
E Work uses live accounts for emails for all employees. Can we use the outlook calendar to give the employees an universal work schedule calendar? Using Outlook 1
witzker Macro to move @domain.xx of a Spammail to Blacklist in Outlook 2019 Outlook VBA and Custom Forms 7
D Outlook 2007 on 365 Using Outlook.com accounts in Outlook 2
S Macro for other actions - Outlook 2007 Outlook VBA and Custom Forms 23
P outlook 2008 search box criteria couldn't be saved Using Outlook 2
pcunite Outlook 2019/O365 Build 13127.20408 errors when using MAPI calls Using Outlook 1
C Outlook with Office365 - search across account, by date rate, in multiple folders - how? Using Outlook 2
D Outlook 2010 Outlook in Windows 10 keeps asking for user name and password repeatedly Using Outlook 14
S Outlook mail adressing stops after first match in GAL Using Outlook 0
A Apply Selected Emails to outlook rules and Run Rules Using Outlook 5
T Changing Sent Items location in Outlook 2019 Using Outlook 0
J How do I disable advertising in Outlook 2019? Using Outlook 13

Similar threads

Top