JavaScript - Snippets
Document.getBookmarkRange
Gets a bookmark's range. Throws an error if the bookmark does not exist.
Document.getBookmarkRangeOrNullObject
Gets a bookmark's range. Returns a null object if the bookmark does not exist.
Document.deleteBookmark
Deletes a bookmark, if it exists, from the document.
Range.getBookmarks
Gets the names all bookmarks in or overlapping the range. A bookmark is hidden if its name starts with the underscore character.
Range.insertBookmark
Inserts a bookmark on the range. If a bookmark of the same name exists somewhere, it is deleted first.
when we insertText, a new range is returned, we need to use that range to insertBookmark.
Word.run(function (context)
{
let range = context.document.getSelection();
return context.sync().then(function ()
{
let insertedTextRange = range.insertText(`Test Bookmark`, Word.InsertLocation.replace);
let uniqueStr = new Date().getTime();
let bookmarkName = `Test_BookmarkCode_${uniqueStr}`;
insertedTextRange.insertBookmark(bookmarkName);
});
});
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrev