How to get table data from email

Hi,

i want to get the details from table dynamically from input email.

used below code, but has some error:
’ Declare the necessary variables
Dim tableHtml As String = emailBody
Dim tableRegex As String = “<table.?>(.?)”
Dim matches As System.Text.RegularExpressions.MatchCollection = System.Text.RegularExpressions.Regex.Matches(tableHtml, tableRegex, System.Text.RegularExpressions.RegexOptions.IgnoreCase)

’ Create and initialize a DataTable to store the extracted data
Dim dt_EmailTable As New DataTable()
dt_EmailTable.Columns.Add(“Work Item”) ’ Add columns as per the table’s structure
dt_EmailTable.Columns.Add(“Months”)
dt_EmailTable.Columns.Add(“WIID”)
’ Add more columns based on the expected table structure

’ Loop through the matches (tables)
For Each match As System.Text.RegularExpressions.Match In matches
’ Get the HTML content inside the table
Dim rowHtml As String = match.Groups(1).Value

' Use Regex to extract rows from the table
Dim rows As System.Text.RegularExpressions.MatchCollection = System.Text.RegularExpressions.Regex.Matches(rowHtml, "<tr.*?>(.*?)</tr>", System.Text.RegularExpressions.RegexOptions.IgnoreCase)

' Loop through each row
For Each row As System.Text.RegularExpressions.Match In rows
    ' Extract columns (td elements) from each row
    Dim columns As System.Text.RegularExpressions.MatchCollection = System.Text.RegularExpressions.Regex.Matches(row.Groups(1).Value, "<td.*?>(.*?)</td>", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
    
    ' Ensure that there are enough columns in this row
    If columns.Count >= 3 Then
        ' Create a DataRow to store the extracted columns
        Dim newRow As DataRow = dt_EmailTable.NewRow()
        
        ' Loop through the columns and populate the DataRow
        For i As Integer = 0 To columns.Count - 1
            ' Trim and assign the value to the DataRow
            newRow(i) = columns(i).Groups(1).Value.Trim()
        Next
        
        ' Add the new row to the DataTable
        dt_EmailTable.Rows.Add(newRow)
    End If
Next

Next

’ Debugging: Verify that dt_EmailTable is populated correctly
Console.WriteLine("Rows in dt_EmailTable: " & dt_EmailTable.Rows.Count)

’ Check if there are any rows before proceeding with the copy
If dt_EmailTable.Rows.Count > 0 Then
’ Now copy dt_EmailTable into dt_Temp
Dim dt_Temp As DataTable = dt_EmailTable.Copy()

' Debugging: Verify that dt_Temp has the same number of rows
Console.WriteLine("Rows in dt_Temp after copy: " & dt_Temp.Rows.Count)

Else
Console.WriteLine(“dt_EmailTable is empty, nothing to copy!”)
End If
error screenshot

Please check below thread

How to extract table data from mail body? - Help / Activities - UiPath Community Forum

@Rakesh_Tiwari

TO Summarize, following are the different options

  1. Using Custom Activities: There are custom activities available in the UiPath Marketplace, such as “Extract Tables from Mail” and “HTML to DataTable,” which can help extract tables from the email body

Extract Tables from Mail - RPA Component | UiPath Marketplace | Overview

.
3. HTML Conversion: Convert the email body to HTML and then use data scraping techniques to extract the table data. This involves saving the email as an HTML file and then using the Data Scraping activity to extract the table

.
4. Manual Extraction: If the above methods do not work, you can manually parse the email body text to extract the table data. This might involve using string manipulation techniques to identify and extract the table content

Hi @Rakesh_Tiwari

The error occurs when a variable named dt_Temp is being declared again within the same method where it is already defined as a parameter. I have changed a line in Invoke Code. Below is the code:

’ Declare the necessary variables
Dim tableHtml As String = emailBody
Dim tableRegex As String = “<table.?>(.?)”
Dim matches As System.Text.RegularExpressions.MatchCollection = System.Text.RegularExpressions.Regex.Matches(tableHtml, tableRegex, System.Text.RegularExpressions.RegexOptions.IgnoreCase)

’ Create And initialize a DataTable To store the extracted data
Dim dt_EmailTable As New DataTable()
dt_EmailTable.Columns.Add(“Work Item”) ’ Add columns As per the table’s Structure
dt_EmailTable.Columns.Add(“Months”)
dt_EmailTable.Columns.Add(“WIID”)
’ Add more columns based On the expected table Structure

’ Loop through the matches (tables)
For Each match As System.Text.RegularExpressions.Match In matches
’ Get the HTML content inside the table
Dim rowHtml As String = match.Groups(1).Value

' Use Regex to extract rows from the table
Dim rows As System.Text.RegularExpressions.MatchCollection = System.Text.RegularExpressions.Regex.Matches(rowHtml, "<tr.*?>(.*?)</tr>", System.Text.RegularExpressions.RegexOptions.IgnoreCase)

' Loop through each row
For Each row As System.Text.RegularExpressions.Match In rows
    ' Extract columns (td elements) from each row
    Dim columns As System.Text.RegularExpressions.MatchCollection = System.Text.RegularExpressions.Regex.Matches(row.Groups(1).Value, "<td.*?>(.*?)</td>", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
    
    ' Ensure that there are enough columns in this row
    If columns.Count >= 3 Then
        ' Create a DataRow to store the extracted columns
        Dim newRow As DataRow = dt_EmailTable.NewRow()
        
        ' Loop through the columns and populate the DataRow
        For i As Integer = 0 To columns.Count - 1
            ' Trim and assign the value to the DataRow
            newRow(i) = columns(i).Groups(1).Value.Trim()
        Next
        
        ' Add the new row to the DataTable
        dt_EmailTable.Rows.Add(newRow)
    End If
Next
Next

’ Debugging: Verify that dt_EmailTable Is populated correctly
Console.WriteLine("Rows in dt_EmailTable: " & dt_EmailTable.Rows.Count)

’ Check If there are any rows before proceeding With the copy
If dt_EmailTable.Rows.Count > 0 Then
’ Now copy dt_EmailTable Into dt_Temp
dt_Temp = dt_EmailTable.Copy()

' Debugging: Verify that dt_Temp has the same number of rows
Console.WriteLine("Rows in dt_Temp after copy: " & dt_Temp.Rows.Count)
Else
Console.WriteLine(“dt_EmailTable Is empty, Nothing To copy!”)
End If

Hope it helps!!

thanks for reply @Parvathy

i tried but not getting any output from the datatable

Hi @Rakesh_Tiwari

Actually MailMessage_Item.Body will print the body content and not HTML content. To get HTML content, you need to use MailMessage_Item.BodyAsHTML to get HTML content string output and check the regular expression you have wrritten.

If possible share the HTML content in Notepad file and I will check your code.

Hope it helps!!

I was dealing with same problem and solved it like follows

Dim dtLocal As system.data.DataTable
dtLocal = dtArgument
' manipulate dtLocal
dtArgument = dtLocal

My dtArgument was in/out argument.

Cheers

Hi @Rakesh_Tiwari

You can easily get the datatable from the Mail Get the html body from the Mail and check this thread

And then you can perform manipulations on the datatable to get particular value using Linq etc

Hope this helps!