How to read table in a Word document

word

#1

Is it possible to read a table in a section of a Word document using UiPath. Can this be done using any of the existing Activities available in UiPath or should we download any third-party activity?


#2

Hi Skumar,

Yes, it is possible to read a table using the Read text activity in the Word application scope. But the output of this will be a string and not a datatable.

If you wish to copy the data table as a table, use the Ctrl+G shortcut, go to table and use Ctrl+C to copy the table. You can then paste it to excel and use Excel based activities to get it out as a data table.

Regards,
Amrita


Word Table data to Excel
Extracting table from Word file
#3

I like the solution you have provided and by using CTRL+G i can go to a table.But one has to select a table even before sending a hot key of CTRL+C.How can one achieve that?


#4

@Kusum_R if there is an activity where you can pass the file path and Table number (1 or 2 or 3) and get comma saperated string as output? Will that help you?

As a last resort, you may create a custom activity (if it is very important)

namespace WordTable
{
    public class WordTable : CodeActivity
    {
        [Category("Input")]
        [RequiredArgument]
        public InArgument<string> FilePath { get; set; }

        [Category("Input")]
        [RequiredArgument]
        public InArgument<int> TableNumber { get; set; }

        [Category("Output")]
        public OutArgument<Word.Table> Output { get; set; }

        protected override void Execute(CodeActivityContext context)
        {
            Word.Application wordApp = new Word.Application();
            object filename = FilePath.ToString();
            object missing = Type.Missing;
            Word.Document doc = wordApp.Documents.Open(ref filename, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            Word.Table table = doc.Tables[int.Parse(TableNumber.ToString())]; //define this index depending on the number of table which you want to get
            doc.Close(ref missing, ref missing, ref missing);
            Marshal.ReleaseComObject(wordApp);

            Output.Set(context, table);
        }

    }
}

#5

Hi vvaidya,

      Could you provide details of required Assemblies & References for the above code.

Thanks in advance.

Best
Shreekrishna


#6

try this

ui_word_tables.xaml (11.5 KB)


#7

Thank you @vvaidya.

 I tried & it's working for me

#8

Fyi…You will have to import and add below assembly to your xaml file (open in text).

<AssemblyReference>Microsoft.Office.Interop.Word</AssemblyReference>


#9

How To Save that Table 1 Value in Text Format ?


#10

wordDoc.Tables(1).ConvertToText.Text


#11

Sir I Want To Store That Value Showing in last Message Box in a Text File ?
So Which Activities used to store that value ?


#12

Use Write Text file actvity. Pass your file path and the Value (in messagebox) into it.


#13

But I Used It, But I Got Like This


#14


#15

hi Amrita,

Could you please suggest how to open the word document and read the TEXT into it and do further operation.
I am not finding any activity to do so…
We have Word App Scope it opens the file and I am not able to perform Read Text activity, as I need to look for some text into the file, how I can know the text is found and do the further operation.

Thanks in advance!


#16

adding to it, once we give file path in word app scope, file is open and when read activity executes it finds the file as Locked and bot failed.

please suggest!

thanks!


#17

@vvaidya, thanks for this solution, it helps indeed. Will it possible to output the result as datatable instead? as it is easier to work with datatable, e.g port to excel, etc2.

also do you have any idea why my studio unable to complete this job even thought i got all the message box? its stuck in running with winword.exe keep running (manually kill this exe also does not stop the task)


#18

Probably some resource is being hold by invoke code, use the attached code instead.

One simple way to convert to dt using existing activities is writing to CSV and reading as Datatable (included).

OR you can also create a datatable and use for each to write each cell to row.

Works for you if you are lucky.:wink:

wordTables.xaml (9.5 KB)


#19

@vvaidya, thanks again. Just got back from holiday, finished testing it now, no more hanging, and it exported to dt successfully.

But there is one flaw, as reading by Word is separate by ‘-’, i have cell value which contain ‘-’, thus it is dividing them incorrectly. any way to work around the ‘-’ split?

Update: found another flow, even though no more hanging, but the word still not yet close properly. Manual workaround is to kill winword.exe manually.

Update: another issue. if the value of the cell is 1,088 --> it will be split to 2 column under csv, 1 and 88 (instead of 088).

also i also change to read using Excel instead of CSV as my table structure is not fixed.


#20

foreach (Word.Row row in wordTable.Rows)

{

List
cellValues = new List()

foreach (Word.Cell cell in row.Cells)

{

   string cellContents = cell.Range.Text;

 ----- Add cell to  cellValues

list

}

----- Pass cellValues list.ToArray
to add data row activity here

}

I suggest the simple way using existing activities.If you are having issues with formatting, probably you have to use for each and add it to a Datatable.

Thanks,

Vinay