How to get Current Page or Active Bookmark

In Microsoft Word, we can use the following VBA command to select the current page where the cursor is placed:
ActiveDocument.Bookmarks(“\Page”).Select
This is useful for getting the entire page’s content programmatically. I’m trying to achieve something similar in ONLYOFFICE — either by selecting:

  • The current page based on cursor location, or
  • A specific bookmark’s content

Is there a way to do this in ONLYOFFICE using macros, API, or plugins?

Any example or documentation reference would be greatly appreciated.

Hello @subbu,

Getting the current page is possible with this method:

(note that it returns a JS index number so the first page will have an index 0)

(function()
{
console.log (Api.GetDocument().GetCurrentPage())
})();

Selecting a specific bookmark’s content is also possible:

(function()
{
Api.GetDocument().GetBookmarkRange("mybookmark").Select()
})();
1 Like

Thanks @carl
I have a 6-page document, with each page having a bookmark. I need to select bookmarks for specific pages — for example, I want to select the bookmarks on pages 2 and 3.

(function () {
    const document = Api.GetDocument();

    // Define the pages you want to select bookmarks for
    const pagesToSelect = [2, 3];

    pagesToSelect.forEach(pageNumber => {
        const bookmarkName = `page_${pageNumber}`;
        const range = document.GetBookmarkRange(bookmarkName);

        if (range) {
            range.Select();
            console.log(`Selected bookmark: ${bookmarkName}`);
        } else {
            console.warn(`Bookmark not found: ${bookmarkName}`);
        }
    });
})();