We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to insert text into table's cell using VB.NET

HI

How to add values to Cells in the below table created

Aspose.Words.Tables.Table table1 = (Aspose.Words.Tables.Table)doc.GetChild(NodeType.Table, 0, true);

Hi Ajeesh,

Thanks for your query. Please read following forum link for your kind reference. Hope this helps you. Please let me know, If you have any more queries.

Hi

For ur reply message when i am click on link am getting Access denied message

Hi Ajeesh,

Thank you for inquiry. Sorry of inconvenience. It is marked as private. please follow up the code snippet below:

Dim doc As Document = New Document(“Test005\in.doc”)

‘Get table from the docuemnt

Dim tab As Tables.Table = doc.FirstSection.Body.Tables(0)

‘edit text in some of cells

InsertTextIntoCell(tab.Rows(0).Cells(0), “This is new text”, True, False) 

InsertTextIntoCell(tab.Rows(1).Cells(1), “This is new text”, False, False) 

InsertTextIntoCell(tab.Rows(2).Cells(2), “This is new text”, False, True) 

InsertTextIntoCell(tab.Rows(0).Cells(2), “This is new text”, True, True) 

‘Save output docuemnt
doc.Save(“Test005\out.doc”)

 Private Sub InsertTextIntoCell(ByVal cell As Tables.Cell, ByVal text As String, ByValremoveOldContent As Boolean, ByVal isNewLine As Boolean)



'if there is no paragraphs in the cell

'we shold add new line

If (cell.Paragraphs.Count = 0) Then

isNewLine = True

removeOldContent = False

End If



'Remove old content from the cell if it is needed

If (removeOldContent) Then

'If we remove old content isNewLine option will be ignored

isNewLine = False



'get empty paragraph

Dim para As Paragraph = GetEmptyParagraph(cell)



‘Remove all content from cell

cell.RemoveAllChildren()



‘Add empty paragraph

cell.AppendChild(para)

End If



‘add new paragrapg if it is needed

If (isNewLine) Then

cell.AppendChild(GetEmptyParagraph(cell))

End If



‘get last Run of last paragraph in the cell

Dim run As Run = cell.LastParagraph.Runs(cell.LastParagraph.Runs.Count - 1)



‘Appent text to last run

run.Text = run.Text & text



End Sub

 Private Function GetEmptyParagraph(ByVal cell As Tables.Cell) As Paragraph



Dim para As Paragraph



'if cell alrady contains paragraphs we should return clone of last paragraph

'That contains one empty run (clone of first run of this paragraph)

'this is needed to preserve formating of cell

If (cell.Paragraphs.Count > 0) Then

'Clone first paragraph of the cell

'this is needed to preserve formaing

para = CType(cell.LastParagraph.Clone(False), Paragraph)



'clone first run of the paragraph

Dim run As Run = CType(cell.LastParagraph.Runs(0).Clone(True), Run)

run.Text = String.Empty

para.AppendChild(run)

Else

'If cell does not contain paragraphs we will return just empty paragraph

para = New Paragraph(cell.Document)

Dim run As Run = New Run(cell.Document)

para.AppendChild(run)

End If



Return para



End Function

In case of any ambiguity, please let me know.