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

Free Support Forum - aspose.com

Find table cell index by text, not bookmark

Hi,
I have this code that I use to find a table using a Word bookmark. Now I need the same function but without having a Word bookmark, but a piece of text, ie [Start].
Can you help me, please?

builder.MoveToBookmark(bookmark)

            Dim sCell = DirectCast(builder.CurrentNode.GetAncestor(NodeType.Cell), Tables.Cell)
            Dim sTable = DirectCast(sCell.GetAncestor(NodeType.Table), Tables.Table)
            Dim sect = DirectCast(sTable.GetAncestor(NodeType.Section), Section)
            Dim sRow = DirectCast(sCell.GetAncestor(NodeType.Row), Tables.Row)

            sectIndex = word.Sections.IndexOf(sect)
            tableIndex = word.Sections(sectIndex).Body.Tables.IndexOf(sTable)
            rowIndex = sTable.IndexOf(sRow)
            cellIndex = sRow.IndexOf(sCell)

@JPGP,

You can build logic on the following code to find the Table in Word document (containing the search string):

Document doc = new Document("E:\\Temp\\in.docx");

FindReplaceOptions options = new FindReplaceOptions()
{
    Direction = FindReplaceDirection.Backward,
    ReplacingCallback = new FindAndReplace1()
};
doc.Range.Replace("[Start]", "[Start]", options);


doc.Save("E:\\Temp\\20.3.docx");

private class FindAndReplace1 : IReplacingCallback
{
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {

        Table table = (Table) e.MatchNode.GetAncestor(NodeType.Table);
        if (table != null)
        {
            table.FirstRow.FirstCell.CellFormat.Shading.BackgroundPatternColor = Color.Gray;
        }

        return ReplaceAction.Skip;
    }
}

P.S. You can easily convert the above C# code to VB.NET yourself by using some converter. For example: https://www.developerfusion.com/tools/convert/csharp-to-vb/