Hi @Constantine I want to insert a bookmark in the current position of the mouse, and then set the inserted bookmark as a bookmark,Here’s how I did it:
I don’t quite understand this. How is that? Do you want to insert some text as an anchor for a bookmark? Can you share an example how this can be done manually via editor interface? I’m not talking about macros or plugin, I’d like to understand scenario how it is.
This is an incorrect example by its concept. You are building a paragraph from scratch, which means that it is not in the document yet, and after that trying to get a range from undefined paragraph. Also, there is no text string that would define bookmark name.
In general, this approach with insertion of new paragraph won’t work.
I came up with following approach:
{
var oName = "Bookmark";
var bookmarkName = oName.toString().trim();
Asc.scope.bookmarkName = bookmarkName;
window.Asc.plugin.callCommand(function (bookmarkName) {
var oDocument = Api.GetDocument();
var oSent = oDocument.GetCurrentSentence("before");
console.log(oSent);
var aSearch = oDocument.Search(oSent);
var p = aSearch[0].GetEndPos();
console.log(p);
var newRage = aSearch[0].GetRange(p - 1, p);
newRage.AddBookmark(Asc.scope.bookmarkName);
}, false, bookmarkName);
}
Please note that you need to somehow pass a sting for the bookmark name. In this example I’ve added a variable for it for the reference.
This simply gets the range with Search that looks up for the result of GetCurrentSentence with before parameter, parses returned range with GetEndPost method to get ending index of the range and start new range for the insertion of a bookmark. I’ve calculated minus one character for new range with p - 1, because range for bookmark insertion cannot be zero characters length.