Office Scripts

At the moment there is no WordScript object model.
You have to use Office Add-ins which is a superset of Office Scripts.


Add-ins - getBookmarkRange

Document.getBookmarkRange 

Gets a bookmark's range. Throws an error if the bookmark does not exist.


Add-ins - getBookmarkRangeOrNullObject

Document.getBookmarkRangeOrNullObject 

Gets a bookmark's range. Returns a null object if the bookmark does not exist.


Add-ins - deleteBookmark

Document.deleteBookmark 

Deletes a bookmark, if it exists, from the document.


Add-ins - Range.getBookmarks

Gets the names all bookmarks in or overlapping the range. A bookmark is hidden if its name starts with the underscore character.


Add-ins - 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 TopPrevNext