Bookmarks
Add, delete, find, show, and style DOCX bookmarks with editor commands, hooks, and the document API.
A bookmark names a span of a document, or one point in it. Internal links, cross-reference fields, and tables of contents point at bookmarks. The editor keeps every bookmark that a document declares.
Add and delete bookmarks
Run these commands to change bookmarks. Each write is one undo step:
editor.exec({ type: 'insertBookmark', name: 'Intro' });
editor.exec({ type: 'deleteBookmark', name: 'Intro' });
const bookmarks = editor.query({ type: 'bookmarks' });A name starts with a letter, holds only letters, digits, and underscores, and has at most 40 characters. Names compare without case. Reusing a name moves that bookmark to the selection, even from another story.
| Command | Purpose | Refusal codes |
|---|---|---|
insertBookmark | Adds or moves a bookmark at the selection. | invalidArgs for an invalid name |
deleteBookmark | Removes a bookmark. The text stays. | notFound |
goToBookmark | Selects the bookmark and scrolls to it. | notFound |
keepFirstBookmark | Removes duplicate copies of a name. | notFound, unsupported with no copies |
toggleBookmarks | Shows or hides bookmark marks. | None |
openBookmarkDialog | Opens the registered bookmark popup. | unsupported with no registered dialog |
openGoTo | Opens the registered Go To UI. | unsupported with no navigation pane |
The bookmarks query lists every bookmark in document order, hidden ones included. Each entry has name, hidden, story, order, empty, atSelection, and duplicates. A document open for viewing refuses writes and accepts goToBookmark.
Find and go to bookmarks
Run goToBookmark to jump without UI, or openGoTo to open the navigation pane's Bookmarks tab:
editor.exec({ type: 'goToBookmark', name: 'intro' });
editor.exec({ type: 'openGoTo' });A jump selects the bookmark and opens the header, footer, or note that holds it. In a read-only editor, a jump into a header, footer, or note scrolls it into view without opening the story or moving the caret.
In a document script, use the Office.js-shaped document API:
const range = context.document.getBookmarkRangeOrNullObject('Intro');
await context.sync();
if (!range.isNullObject) {
range.select();
await context.sync();
}range.select() refuses with NotSupported in a server runtime. A read-only editor also refuses it for a header, footer, or note. For the document API members, see Search and navigation.
Build bookmark controls
These hooks and registrations back custom bookmark UI:
| API | Purpose |
|---|---|
useBookmarks() | The bookmark list and insert, remove, and goTo actions. See React hooks and Vue composables. |
useGoToBookmark() | Typed text, matching rows, the highlighted row, move(delta), and go(name?). |
editor.setBookmarkChrome({ onRequest }) | Registers a dialog for openBookmarkDialog. Returns an unregister function. |
editor.setGoToChrome({ onRequest }) | Registers a Go To UI for openGoTo. |
popups.bookmark | Replaces the packaged dialog. false removes it. |
insert.goToBookmark | Menu slot that runs openGoTo. Disabled while no navigation pane is mounted. |
openGoToandopenBookmarkDialogrequire registered UI. The packaged editor registers thebookmarkpopup.- If your Go To UI registers with
shortcut: false,{ type: 'openGoTo', fromShortcut: true }refuses withunsupported.
For popup composition, see Customize popups. For the navigation pane, see Navigate a document.
Show and style bookmark marks
Call editor.setBookmarkMarks(options) to choose and decorate marks. It returns a function that restores the previous options:
const restore = editor.setBookmarkMarks({
include: (bookmark) => bookmark.name.startsWith('Sec'),
decorate(element, mark) {
element.title = mark.name;
element.style.pointerEvents = 'auto'; // Marks ignore the pointer by default.
},
});
restore();| Option | Values |
|---|---|
include | 'visible' (default) skips hidden bookmarks. 'all' marks all. A function receives name, hidden, and story. |
decorate(element, mark) | Changes or replaces a mark element. mark has name, hidden, story, and edge. |
print | true keeps marks when the browser prints. |
If decorate throws, the editor keeps the default mark and emits an error event with code hostCallback. editor.snapshot().showBookmarks reports whether marks show.
Set these tokens on .docx-editor to style the marks:
| Token | Controls |
|---|---|
--doc-bookmark-mark | Mark color. Default gray. |
--doc-bookmark-mark-width | Stroke width. Default 1px. |
--doc-bookmark-mark-size | Bracket arm width at 100% zoom. Default 3px. |
--doc-bookmark-point-glyph | A CSS background that replaces the point I-beam. |
Each mark has the class docx-bookmark-mark and the attributes data-bookmark-name, data-bookmark-hidden, data-bookmark-edge, and data-bookmark-story.
Resolve duplicate names
Concurrent edits in a collaboration session can add the same name in two places. Both bookmarks remain, and every participant uses the first one in document order. The duplicates field counts the copies.
Run keepFirstBookmark to keep the first bookmark and remove every copy in one undo step:
editor.exec({ type: 'keepFirstBookmark', name: 'Intro' });The Bookmark dialog offers the same action as Keep first. Deleting the name also removes every copy.
Limits
- Bookmark marker changes are direct changes, including in suggesting mode and under
TrackMineOnly. - Moving or deleting protected markers, or markers inside data-bound controls, refuses the whole change.
- Replacing the complete bookmark range, by typing or
insertText(text, 'Replace'), removes the bookmark. - Deleting part of the text keeps the bookmark around the rest. A tracked deletion keeps every bookmark.
- Marks never enter exports, the document, or undo history. Printing marks requires
print: true. - Automation
select()refuses unsupported viewing stories and server hosts withNotSupported.
Edits at bookmark edges follow these rules:
| Edit | Result |
|---|---|
| Line break at a bookmark's start | Goes inside the bookmark. |
| Line break at a bookmark's end | Stays outside the bookmark. |
| Text inserted at a point bookmark | Goes after the point. |
| Replacement over a selection that crosses a bookmark's start | Goes inside the bookmark. |
| Replacement over a selection that crosses a bookmark's end | Stays outside the bookmark. |
| Paragraph split at a bookmark start or point | Keeps that marker in the first paragraph. |
| Deletion that touches a point bookmark | Removes it only when the point lies strictly inside the deleted range. |
| Accepted tracked deletion | Keeps the markers where the deleted text was. |