adam
adam

Reputation: 501

How to extract page numbers from Word document via add-in built using the Word JavaScript API

in Word via an add-in built using the Word Javascript API, I'm trying to read the page number of each location a string of text appears in the document text. E.g. the word "HELP" appears on pages 2, 6, 10 and 45. How can I read the page number?

A stripped down version of the code I have so far is:

async function run() {
  await Word.run(async (context) => {
    let paragraphs = context.document.body.paragraphs;
    paragraphs.load("text"); // WHAT ELSE NEEDS TO BE LOADED TO ALLOW PAGE NO EXTRACTION?
    await context.sync();
    // loop through document to find text
    paragraphs.items.forEach((item) => {
      let paragraph = item.text.trim();
      if (paragraph.includes("EXAMPLE STRING")) {
        let pageNumber = what_goes_here?;
        console.log("Text found on page: " + pageNumber);
      }
    });
  });
}

This works fine for finding the text string, but I can't work out how to get the page number (pageNumber in the code above).

I've been through the Word API documentation but can't find anything. I can, for example, where the text is in a list extract the list level by loading the "listItemOrNullObject" object and reading the "listString", but I can't see any way to get the page number.

Any ideas? If this isn't available via the standard Word API (as I'm suspecting) has anyone got any workarounds?

I'd like to do this using JS and the Word API (great cross platform support etc.) and not have to use VBA, C# etc.

Thank you!

Upvotes: 1

Views: 1037

Answers (1)

Wenjun Gong
Wenjun Gong

Reputation: 21

Currently there hasn't been such an JS API to return the page number of the selected text. You can post the API request here: https://techcommunity.microsoft.com/t5/microsoft-365-developer-platform/idb-p/Microsoft365DeveloperPlatform

Wenjun

Upvotes: 1

Related Questions