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?
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
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?
@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);
}
}
}
Hi vvaidya,
Could you provide details of required Assemblies & References for the above code.
Thanks in advance.
Best
Shreekrishna
try this
ui_word_tables.xaml (11.5 KB)
Thank you @vvaidya.
I tried & it's working for me
Fyi…You will have to import and add below assembly to your xaml file (open in text).
<AssemblyReference>Microsoft.Office.Interop.Word</AssemblyReference>
How To Save that Table 1 Value in Text Format ?
wordDoc.Tables(1).ConvertToText.Text
Sir I Want To Store That Value Showing in last Message Box in a Text File ?
So Which Activities used to store that value ?
Use Write Text file actvity. Pass your file path and the Value (in messagebox) into it.
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!
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!
@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)
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.
wordTables.xaml (9.5 KB)
@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.
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