Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

page number #2111

Open
XWANG0224 opened this issue Oct 24, 2024 · 4 comments
Open

page number #2111

XWANG0224 opened this issue Oct 24, 2024 · 4 comments
Assignees
Labels
Area: Word Feedback on Word content Needs: author feedback Waiting for author (creator) of Issue to provide more info

Comments

@XWANG0224
Copy link

is there any way I can get the page number where a selected paragraph is loacated?
and is there any way I can add the page number into the footer

@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs: triage 🔍 new issue, needs PM on rotation to triage asap label Oct 24, 2024
@AlexJerabek
Copy link
Contributor

Hi @XWANG0224,

Thank you for the questions. @ElizabethSamuel-MSFT, are these scenarios supported in the Word API at this time?

@AlexJerabek AlexJerabek added Area: Word Feedback on Word content Needs: attention 👋 Waiting on Microsoft to provide feedback and removed Needs: triage 🔍 new issue, needs PM on rotation to triage asap labels Oct 25, 2024
@ElizabethSamuel-MSFT ElizabethSamuel-MSFT added Needs: author feedback Waiting for author (creator) of Issue to provide more info and removed Needs: attention 👋 Waiting on Microsoft to provide feedback labels Oct 28, 2024
@ElizabethSamuel-MSFT
Copy link
Contributor

@XWANG0224 Thanks for your question. For a dynamic solution, you can use the Page field.

For example, the following code inserts a page field before the selected text in the document. The user should see the number of the current page if the field is set to show the result instead of the code.

async function rangeInsertPageField() {
  // Inserts a Page field before selection.
  await Word.run(async (context) => {
    const range: Word.Range = context.document.getSelection().getRange();

    const field: Word.Field = range.insertField(
      Word.InsertLocation.before,
      Word.FieldType.page
    );

    field.load("result,code");
    await context.sync();

    if (field.isNullObject) {
      console.log("There are no fields in this document.");
    } else {
      console.log("Code of the field: " + field.code, "Result of the field: " + JSON.stringify(field.result));
    }
  });
}

Console logs

In this case, I'd selected text on the second page of my document.

Code of the field:  PAGE  \* MERGEFORMAT 

Result of the field: {"hyperlink":"","isEmpty":false,"style":"Normal","styleBuiltIn":"Normal","text":"2"}

In Script Lab, you can use the Manage fields sample to experiment with the JS field APIs.

You can try something like the following to insert the Page field in the footer.

async function addFooter() {
  await Word.run(async (context) => {
    context.document.sections
      .getFirst()
      .getFooter("Primary")
      .insertText(
        "",
        Word.InsertLocation.replace)
      .getRange()
      .insertField(
        Word.InsertLocation.replace,
        Word.FieldType.page
      );

    await context.sync();
  });
}

In Script Lab, you can use the Insert headers and footers sample to experiment with header/footer APIs.

For more about the Page field, see Field codes: Page field.

For more about fields in general, see Insert, edit, and view fields in Word. Note that the client/platform availability of the Page field depends on if it's supported in the Word UI on that client/platform.

Let us know if this helps or if you have further questions.

Thanks.

@XWANG0224
Copy link
Author

thanks for updating, there is another question about the field code. When I do the cross reference in word,
image
for example, in this screen shot, I found that when I cross reference a numbered list, word will insert a field code, but it's not the name of book mark, instead, it's something like an ID, "_Ref181353778", is there any way I can get this ID by API? Or, is it possible to put the cross reference function into my own word add-in?

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs: attention 👋 Waiting on Microsoft to provide feedback and removed Needs: author feedback Waiting for author (creator) of Issue to provide more info labels Nov 1, 2024
@ElizabethSamuel-MSFT
Copy link
Contributor

@XWANG0224 Thanks for letting us know that my response helped.

As for the cross-reference field showing the code instead of the result, try the following in the Word UI.

  1. Open the context menu (e.g., by right-clicking on the field).
  2. Select the option to "Toggle Field Codes".

Instead of seeing the field's code, you should now see the results - 123 in your case.

As for using the ref field in your add-in code, you should be able to adapt the rangeInsertPageField function provided in my earlier response. For example, instead of FieldType.page, use FieldType.ref. When you load the field's result and code properties, you should get the information you're looking for.

For more about the Ref field, see Field codes: Ref field.

Hope this helps.

@ElizabethSamuel-MSFT ElizabethSamuel-MSFT added Needs: author feedback Waiting for author (creator) of Issue to provide more info and removed Needs: attention 👋 Waiting on Microsoft to provide feedback labels Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Word Feedback on Word content Needs: author feedback Waiting for author (creator) of Issue to provide more info
Projects
None yet
Development

No branches or pull requests

3 participants