Hi @フนӥăіḑ Ṁ',
To get the last page in a Word document body using the Word JavaScript API, you can use the getRange() method of the Body object to get the range of the entire body, and then use the getOoxml() method of the range object to get the OOXML representation of the body. You can then parse the OOXML to get the number of pages in the document and use the getRange() method again to get the range of the last page.
Here's an example code snippet:
Word.run(function (context) {
var body = context.document.body;
var range = body.getRange("Whole");
return context.sync()
.then(function () {
var ooxml = range.getOoxml();
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(ooxml.value, "text/xml");
var pages = xmlDoc.getElementsByTagName("w:lastRenderedPageBreak");
var lastPage = pages[pages.length - 1].getAttribute("w:id");
var lastPageRange = body.getRange("End");
lastPageRange.select();
return context.sync();
});
});
This code gets the range of the entire body, gets the OOXML representation of the range, parses the OOXML to get the number of pages, gets the range of the end of the body, and selects the last page.
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.