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.

CommandPurposeRefusal codes
insertBookmarkAdds or moves a bookmark at the selection.invalidArgs for an invalid name
deleteBookmarkRemoves a bookmark. The text stays.notFound
goToBookmarkSelects the bookmark and scrolls to it.notFound
keepFirstBookmarkRemoves duplicate copies of a name.notFound, unsupported with no copies
toggleBookmarksShows or hides bookmark marks.None
openBookmarkDialogOpens the registered bookmark popup.unsupported with no registered dialog
openGoToOpens 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:

APIPurpose
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.bookmarkReplaces the packaged dialog. false removes it.
insert.goToBookmarkMenu slot that runs openGoTo. Disabled while no navigation pane is mounted.
  • openGoTo and openBookmarkDialog require registered UI. The packaged editor registers the bookmark popup.
  • If your Go To UI registers with shortcut: false, { type: 'openGoTo', fromShortcut: true } refuses with unsupported.

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();
OptionValues
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.
printtrue 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:

TokenControls
--doc-bookmark-markMark color. Default gray.
--doc-bookmark-mark-widthStroke width. Default 1px.
--doc-bookmark-mark-sizeBracket arm width at 100% zoom. Default 3px.
--doc-bookmark-point-glyphA 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 with NotSupported.

Edits at bookmark edges follow these rules:

EditResult
Line break at a bookmark's startGoes inside the bookmark.
Line break at a bookmark's endStays outside the bookmark.
Text inserted at a point bookmarkGoes after the point.
Replacement over a selection that crosses a bookmark's startGoes inside the bookmark.
Replacement over a selection that crosses a bookmark's endStays outside the bookmark.
Paragraph split at a bookmark start or pointKeeps that marker in the first paragraph.
Deletion that touches a point bookmarkRemoves it only when the point lies strictly inside the deleted range.
Accepted tracked deletionKeeps the markers where the deleted text was.

See also