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.
Thread starter Similar threads 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 Retrieving contacts / e-mails from closed profile Using Outlook 1
N need help retrieving email from previous Outlook Profile Using Outlook 7
S Performance while retrieving contacts Using Outlook 1
B Retrieving Archived Files in Outlook Using Outlook 1
R retrieving old messages after trash has been emptied Using Outlook 1
C retrieving contacts in subfolders that have been accidentally deleted 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
L Outlook 2010: cannot insert tables or illustrations Using Outlook 14
W Outlook 2010 open MSG directly that contains HTML Tables and send out. That will lose all of HTML f Using Outlook 1
J Inserting tables in emails using Outlook 2010 (over prior Office 2007) Using Outlook 1
D Outlook 2010 Outlook in Windows 10 keeps asking for user name and password repeatedly Using Outlook 0
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 5
S Outlook (2016 32bit; Gmail IMAP) - Save sent message to Outllook Folder Outlook VBA and Custom Forms 0
L Unable to Sync Web/Android MS To Do with Windows Outlook Tasks Using Outlook 3
S Macro to move “Re:” & “FWD:” email recieved the shared inbox to a subfolder in outlook Outlook VBA and Custom Forms 0
S Outlook Macro to send auto acknowledge mail only to new mails received to a specific shared inbox Outlook VBA and Custom Forms 0
S Outlook Macro to move reply mail based on the key word in the subjectline Outlook VBA and Custom Forms 0
B Outlook Calendar not coming down from cloud Using Outlook 2
Terry Sullivan Sender's Name Doesn't Appear in the From Field on Outlook 365/IMAP Using Outlook 2
GregS Outlook 2016 Can I disable the Outlook Outbox? Using Outlook 2
P Outlook pst file is too huge with POP3. How to save more space? Using Outlook 4
D Outlook Contacts Notes Field Photos to Smartphone Using Outlook 0
Paula S. Outlook 2013 won't recognize new password Using Outlook 4
J Want to learn VBA Macros for Outlook. What book can you recommend? Outlook VBA and Custom Forms 2
A Backup Email Accounts On OutLook For Mac 2016 (Microsoft 365 subscription version) Using Outlook 0
J Time Zone Issues - Outlook Calendar and Webcal Feed Using Outlook 8
J Are the underlined letters in Outlook Reminders window supposed to work? Using Outlook 0
M Office 2016 Outlook is forgetting passwords Using Outlook 14
G How to add a folder shortcut to outlook quick access toolbar? Using Outlook 6
W Outlook WAS facebook but there is no info for old accounts Using Outlook 0
E Can one still buy Outlook (or Office) 2016? Using Outlook 6
Similar threads


















































Top