Bookmarks inside cells

I have a table with cells and bookmarks in the cells.
I want to select the cell and delete the text AFTER bookmark, without deleteing the bookmark,
and fill the the cell with new text AFTER the bookmark.
In VBA is like this

wrd.ActiveDocument.Bookmarks(bm.Name).Select()
wrd.Selection.SelectCell()
wrd.Selection.Text = d1.Item("Dokno").ToString This example is deleting all and insert a new bookmark with text
builder.MoveToBookmark("Dok")
srcBookmark.BookmarkEnd.Range.Delete()
srcDoc.Range.Bookmarks("Dok").Text = "Text"

Do you have a better solution?

Hi Harry,

Thanks for your inquiry. First of all, please note that Aspose.Words is quite different from the Microsoft Word’s Object Model in that it represents the document as a tree of objects more like an XML DOM tree. If you worked with any XML DOM library you will find it is easy to understand and work with Aspose.Words. When you load a Word document into Aspose.Words, it builds its DOM and all document elements and formatting is simply loaded into memory. Please read the following articles for more information on DOM:
https://docs.aspose.com/words/net/aspose-words-document-object-model/
https://docs.aspose.com/words/net/logical-levels-of-nodes-in-a-document/

Secondly, I suggest you please read following documentation links for your kind reference.
https://docs.aspose.com/words/net/document-builder-overview/
https://docs.aspose.com/words/net/navigation-with-cursor/

Please use the following code example to achieve your requirements. If you still face problem, please share your input and expected output documents. We will then provide you more information on this along with code.

Document doc = new Document(MyDir + "Test02.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToBookmark("bm");
ArrayList nodes = new ArrayList();
Cell cell = (Cell) builder.CurrentNode.GetAncestor(NodeType.Cell);
if (cell != null)
{
    Node currentNode = doc.Range.Bookmarks["bm"].BookmarkEnd.NextSibling;
    while (currentNode != cell.LastChild && currentNode != null)
    {
        if (currentNode.NodeType == NodeType.Run)
        {
            nodes.Add(currentNode);
        }
        currentNode = currentNode.NextPreOrder(doc);
    }
}
foreach(Node node in nodes)
{
    node.Remove();
}
Node bmEnd = doc.Range.Bookmarks["bm"].BookmarkEnd.NextSibling;
builder.MoveTo(bmEnd);
builder.Write("New text");
doc.Save(MyDir + "Out.docx");

OK…Yes, I know there is a big difference, but I really like your Product.
To automate Office Products on a server is a hell :slight_smile:
Thanks a lot.

Hi Harry,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

It did not Place the text after bookmark, only inside.
However if I change the code to this it worked:

builder.MoveToBookmark("Dok")
Dim nodes As ArrayList = New ArrayList()
Dim cell As Cell = DirectCast(builder.CurrentNode.GetAncestor(NodeType.Cell), Cell)

If Not cell Is Nothing Then
    Dim currentNode As Node = srcDoc.Range.Bookmarks("Dok").BookmarkEnd.NextSibling
    While Not currentNode Is cell.LastChild And Not currentNode Is Nothing
        If currentNode.NodeType = NodeType.Run Then
            nodes.Add(currentNode)

        End If
        currentNode = currentNode.NextPreOrder(srcDoc)

    End While
End If
For Each node As Node In nodes
    node.Remove()
Next
'Dim bmEnd As Node = srcDoc.Range.Bookmarks("Dok").BookmarkEnd.ParentNode
builder.MoveToBookmark("Dok", False, True)
'builder.MoveTo(bmEnd)
builder.Write("HARRY")

Sorry,It should be like this, of course:)

If Not srcDoc.Range.Bookmarks("Dok") Is Nothing Then
    builder.MoveToBookmark("Dok", False, True)
    'builder.MoveTo(bmEnd)
    builder.Write("HARRY")
End If

Hi Harry,

It is nice to hear from you that your problem has been solved. Please let us know if you have any more queries.