ApiRange - replace text

It is necessary to replace the text received from the bookmark.
I find a bookmark through
var range = document.GetBookmarkRange("Recipient");
I need to replace the text in range with my test.
addText - can add text before or after
Delete - deletes everything, but addText does not insert anything.

How do I do this correctly?

Hello @Duchan
Please clarify your request. Do you want to change bookmark name or the text in the document? Also please take a look at this method:ONLYOFFICE Api Documentation - SearchAndReplace

Do you want to change bookmark name or the text in the document?

“the text in the document”

I’ve done some experiments. Possible option:

oRange = oDocument.GetBookmarkRange("BookmarkName");
oRange2 = oDocument.GetBookmarkRange("BookmarkName");
oRange.AddText("NEW TEXT", "after");
oRange2.Delete();

This code works, but perhaps there is a more correct solution?

I still think that probably SearchandReplace method will be handful in such scenario (change a text in the file):ONLYOFFICE Api Documentation - SearchAndReplace

This is the problem of migrating an existing system that uses MS Office to create documents using templates (insertion locations are defined by bookmarks) to a subsystem for creating documents via DocumentBuilder (using already existing templates).
Therefore, the question was asked specifically about bookmarks.

SearchAndReplace - has both its pros and cons. You can replace several identical “replacement templates” with one value, but this “replacement template” must be unique so that it does not accidentally occur in the general text.
On the other hand, the bookmark can be “stretched” to the standard text by default, and when generating a document, the text will be skipped and the text will be valid

Probably the situation is related to the fact that text which you want to change is stored in the Comment object. Probably this example will be useful:

var oDocument = Api.GetDocument();
var oParagraph = oDocument.GetElement(0);
    oParagraph.AddText("This is just a sample text");
    Api.AddComment(oParagraph, "comment", "John Smith");
var aComments = oDocument.GetAllComments();
    aComments[0].SetText("new comment");
    
    aComments[0].SetText('replace text')
    
    aComments[0].SetText('and again')

I was asking about “Bookmark”. To work with them there are:

  • ApiRange.AddBookmark
  • ApiDocument.DeleteBookmark
  • ApiDocument.GetBookmarkRange
  • ApiDocument.GetAllBookmarksNames

and what does the comments have to do with it?

Sorry, I misunderstood the situation. We’re going to re-check this scenario.
I will update this post when we have something to share.

I do it like this
oRange = oDocument.GetBookmarkRange(“BookmarkName”);
oRange.RemoveAllElements();
oRange.AddText(‘your content’);
this ,ok la