Ok, fine. I have another suggestion. Let’s do not insert anything in the table column except bookmarks, then build tables on fly, and insert them into the template. Please see the following code and attached document:
Public Sub Test004()
Dim doc As Document = New Document("Test004\in.doc")
'insert data into the document
InsertData(doc, "HA47090", New String() {"", "", "3", "", "", ""}, New Integer() {4, 5})
InsertData(doc, "HA75670", New String() {"", "", "", "4", "", ""}, New Integer() {})
InsertData(doc, "HA75688", New String() {"", "", "", "", "5", ""}, New Integer() {})
InsertData(doc, "HB75604", New String() {"", "2", "", "", "", "", "", "", "", ""}, New Integer() {6, 7, 8, 9})
InsertData(doc, "HB75605", New String() {"", "", "", "", "", "", "", "8", "", ""}, New Integer() {})
doc.Save("Test004\out.doc")
doc.SaveToPdf("Test004\out.pdf")
End Sub
'''
''' Generates tabel and insert it at bookmark
'''
''' Destination document
''' Bookmark name
''' Widht of the table
''' Values
''' indexes of undelined cells
'''
Private Sub InsertData(ByVal doc As Document, ByVal bookmarkName As String, ByVal data As String(), ByVal underlinedCells As Integer())
'Check whether bookmark exists
If (doc.Range.Bookmarks(bookmarkName) Is Nothing) Then
Return
End If
'get cell where bookmark is placed
Dim bkCell As Tables.Cell = CType(doc.Range.Bookmarks(bookmarkName).BookmarkStart.GetAncestor(NodeType.Cell), Tables.Cell)
'Calculate width of table cells
Dim cellWidth = bkCell.CellFormat.Width / data.Length
'Build table based on data
Dim tab As Tables.Table = New Tables.Table(doc)
Dim row As Tables.Row = New Tables.Row(doc)
tab.AppendChild(row)
For i As Integer = 0 To data.Length - 1
Dim cell As Tables.Cell = New Tables.Cell(doc)
cell.CellFormat.Width = cellWidth
'Insert text into teh cell
Dim par As Paragraph = New Paragraph(doc)
par.ParagraphFormat.Alignment = ParagraphAlignment.Right
Dim run As Run = New Run(doc, data(i))
par.AppendChild(run)
cell.AppendChild(par)
row.AppendChild(cell)
Next
'Set bottom border where it is needed
For j As Integer = 0 To underlinedCells.Length - 1
If (row.Cells.Count > underlinedCells(j)) Then
row.Cells(underlinedCells(j)).CellFormat.Borders.Bottom.LineStyle = LineStyle.Single
End If
Next
'Insert table ta bookmark
Dim bkNode As Node = doc.Range.Bookmarks(bookmarkName).BookmarkStart.ParentNode
bkNode.ParentNode.InsertAfter(tab, bkNode)
End Sub
Best regards.