How insert /edit Table to a DOCX file without having Microsoft Office installed

Hey Guys,

Did You maybe try insert datable without installing MS Word? I can’t find any information how to do it :frowning:

Hi @Robert_Work ,

Could you let us know some more details on what is to be implemented ?

We could use OpenXml methods to perform the Word document manipulations or creations.

Hi @supermanPunch ,

I need only simply insert DataTable (Dynamicly generated, every time a different number of rows) to Word document for my unattended Robot

So firstly I tried with Word Scope and Insert DatTable in Document with replacing specific text but then it turned out that MS Office should be also on Robot VM installed but I can’t do this.

Hi @Robert_Work ,

Could you maybe provide us with a Sample example of your word document ? This way we can work on a close to case basis solution.

@Robert_Work

You can try something like this

Pass filePath and dt as input arguments in invoke code

Using wordDoc As WordprocessingDocument = WordprocessingDocument.Open(filePath, True)
            Dim mainPart As MainDocumentPart = wordDoc.MainDocumentPart
            If mainPart IsNot Nothing Then
                
                Dim table As New Table()

                Dim tableProperties As New TableProperties(New TableWidth() With {.Width = "5000", .Type = TableWidthUnitValues.Pct})

                
                table.AppendChild(tableProperties)
                Dim headerRow As New TableRow()

               
                For Each col As DataColumn In dt.Columns
                    Dim headerCell As New TableCell(New Paragraph(New Run(New Text(col.ColumnName))))
                    headerRow.AppendChild(headerCell)
                Next

                
                table.AppendChild(headerRow)

                
                For Each dataRow As DataRow In dataTable.Rows
                    Dim dataTableRow As New TableRow()


                    For Each col As DataColumn In dataTable.Columns
                        Dim dataCell As New TableCell(New Paragraph(New Run(New Text(dataRow(col).ToString()))))
                        dataTableRow.AppendChild(dataCell)
                    Next

                    
                    table.AppendChild(dataTableRow)
                Next

            
                Dim firstParagraph As Paragraph = mainPart.Document.Body.Descendants(Of Paragraph)().FirstOrDefault()
                If firstParagraph IsNot Nothing Then
                    firstParagraph.InsertBeforeSelf(table)
                Else                                        
                    mainPart.Document.Body.AppendChild(table)
                End If
            End If

           
            mainPart.Document.Save()
End Using

Cheers

1 Like

Hello @Anil_G Thank You! Your code works. I needed only add this line of code DocumentFormat.OpenXml.Wordprocessing.* to run it from UiPath Studio 2023.4

Using wordDoc As WordprocessingDocument = WordprocessingDocument.Open(filePath, True)
    Dim mainPart As MainDocumentPart = wordDoc.MainDocumentPart
    If mainPart IsNot Nothing Then
        Dim table As New DocumentFormat.OpenXml.Wordprocessing.Table()
        Dim tableProperties As New DocumentFormat.OpenXml.Wordprocessing.TableProperties(New TableWidth() With {.Width = "5000", .Type = TableWidthUnitValues.Pct})
        
        table.AppendChild(tableProperties)
        Dim headerRow As New DocumentFormat.OpenXml.Wordprocessing.TableRow()
        
        For Each col As DataColumn In dt.Columns
            Dim headerCell As New DocumentFormat.OpenXml.Wordprocessing.TableCell(New DocumentFormat.OpenXml.Wordprocessing.Paragraph(New DocumentFormat.OpenXml.Wordprocessing.Run(New DocumentFormat.OpenXml.Wordprocessing.Text(col.ColumnName))))
            headerRow.AppendChild(headerCell)
        Next
        
        table.AppendChild(headerRow)
        
        For Each dataRow As DataRow In dataTable.Rows
            Dim dataTableRow As New DocumentFormat.OpenXml.Wordprocessing.TableRow()
            
            For Each col As DataColumn In dataTable.Columns
                Dim dataCell As New DocumentFormat.OpenXml.Wordprocessing.TableCell(New DocumentFormat.OpenXml.Wordprocessing.Paragraph(New DocumentFormat.OpenXml.Wordprocessing.Run(New DocumentFormat.OpenXml.Wordprocessing.Text(dataRow(col).ToString()))))
                dataTableRow.AppendChild(dataCell)
            Next
            
            table.AppendChild(dataTableRow)
        Next
        
        Dim firstParagraph As DocumentFormat.OpenXml.Wordprocessing.Paragraph = mainPart.Document.Body.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Paragraph)().FirstOrDefault()
        If firstParagraph IsNot Nothing Then
            firstParagraph.InsertBeforeSelf(table)
        Else
            mainPart.Document.Body.AppendChild(table)
        End If
    End If
    mainPart.Document.Save()
End Using

It added DataTable passed to Inoke Code at the beginning of the Word Document.

I only don’t understand why once You use variable dt.Columns and other time dataTable.Columns in Your code. I tested it and this can be the same value :thinking:

1 Like

@Robert_Work

Glad it resolved…

That was a typo…it should be same across…I used as datatable first…but not to conflict with the reserved keyword datatable I changed to dt but I changed only in one place…

Please mark it close the loop so that others with similar issue can get some help

Happy Automation

Cheers

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.