Accessing the value in a table cell - am I going mad?

Hi, Just created a table using the builder and now need to go back through the rows starting from the bottom checking if the first cell in each row contains nothing.

The line below (table is the current table object, l is a valid index for the current row):

table.Rows(l).FirstCell.Range.Text <> “”

does not return “” for an empty cell but instead returns a single unicode string with the value of 7

Why does an empty cell have this value and what it the correct way to access the value in a cell without using “range”.

Even once this strange problem has been resolved, I still need to actually put a value in an existing cell and yet there is no example of doing such a basic operation. At the moment I seem to have to use Range.replace to do this! Why can’t I use the text property of the cell directly? I know how to insert values in cells at table creation time with the builder eg. builder.write but as already stated, the table has already been created and now I must go back through this table to insert values in some empty cells.

I appreciate that a cell could contain multiple objects, but in my case they would only contain a single paragraph.

Help would be very much appreciated…Thank you

Just wanted to add that this is using the very latest version of Words.Net downloaded today.

Also please see attached a little screen shot. It shows the value of a cell which has some text but with the un-printable character on the end.

Below you will see the code to replace a cell containing “” with a total, but of course this doesn’t work because the cell does not just contain “” but must have this strange character also on the end. Is this a paragraph marker or something?

Should I be accessing the text some other way?

Hi Martin,

Thanks for your inquiry.

*msutherland25:

table.Rows(l).FirstCell.Range.Text <> “”
does not return “” for an empty cell but instead returns a single unicode string with the value of 7
Why does an empty cell have this value and what it the correct way to access the value in a cell without using “range”.*

The Range class represents a contiguous area in a document. The document is represented by a tree of nodes and the nodes provide operations to work with the tree, but some operations are easier to perform if the document is treated as a contiguous sequence of text.

Range is a “facade” interface that provide methods that treat the document or portions of the document as “flat” text regardless of the fact that the document nodes are stored in a tree-like object model.

The Range.Text Property gets the text of the range. The returned string includes all control and special characters as described in ControlChar.

In your case, I suggest you please use Cell.ToString(SaveFormat.Text) method to get the text of table’s cell.

*msutherland25:

I still need to actually put a value in an existing cell and yet there is no example of doing such a basic operation. At the moment I seem to have to use Range.replace to do this! Why can’t I use the text property of the cell directly? I know how to insert values in cells at table creation time with the builder eg. builder.write but as already stated, the table has already been created and now I must go back through this table to insert values in some empty cells.*

Please use the following code example to insert text inside empty and non empty table’s cells.

var doc = new Document(MyDir + "Test01.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);
foreach (Row row in table.Rows)
{
    if (row.FirstCell.ToString(SaveFormat.Text).Trim() == "")
    {
        builder.MoveTo(row.FirstCell.FirstParagraph);
        builder.Write("Empty Cell");
    }
    else if (row.FirstCell.ToString(SaveFormat.Text).Trim() != "")
    {
        row.FirstCell.RemoveAllChildren();
        row.FirstCell.EnsureMinimum();
        builder.MoveTo(row.FirstCell.FirstParagraph);
        builder.Write("This Cell is not empty");
    }
}
doc.Save(MyDir + "Out.docx");

*msutherland25:

Below you will see the code to replace a cell containing “” with a total, but of course this doesn’t work because the cell does not just contain “” but must have this strange character also on the end. Is this a paragraph marker or something?

Should I be accessing the text some other way?*

Please use the Cell.ToString(SaveFormat.Text) method to get the text of table’s cell as shown in following code example. You can also use Range.Replace method to replace the text of a table’s cell. Please read following documentation link for your kind reference.
https://docs.aspose.com/words/net/working-with-tables/
Please check the following cod example for your kind reference. Hope this helps you. If you still face any issue, please share your input and expected output document here for testing. I will investigate the issue on my side and provide you more information.

var doc = new Document(MyDir + "Test01.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);
foreach (Row row in table.Rows)
{
    if (row.FirstCell.ToString(SaveFormat.Text).Trim() == "")
    {
        builder.MoveTo(row.FirstCell.FirstParagraph);
        builder.Write("Empty Cell");
    }
    else if (row.FirstCell.ToString(SaveFormat.Text).Trim() != "")
    {
        row.FirstCell.Range.Replace("test", "new text", true, true);
    }
}
doc.Save(MyDir + "Out.docx");