1

How do we change the page size of a section in Office.js? There is no option in the API (latest version). I have tried doing it through OOXML using several different schemes, to no avail.

I have confirmed (by editing a Docx file manually) that you need to change w:w and w:h attributes of w:pgSz node to change the page size. However, when inserting OOXML, I really can't figure out which object I need to insert it. For example I have tried context.document.body.insertOoxml() as well as context.document.sections.getFirst().body.insertOoxml(), both of which do not change page size in any way.

I have also tried using Section.set() function without success.

Can anyone share how to achieve this?

Edit

The root of the problem seems to be that page size is stored in sectPr node, which Office.js never load. I have tried load() function as well as context.loadRecursive(). I can load Sections collection and their scalar and navigation properties just fine, but properties of the section are never loaded. I'm not sure what else I can do.

The other method, that of inserting raw OOXML, also fails because insertOoxml() method is not available with the Section object, but only with Section.body, which affects the contents of the section and not its meta properties.

6
  • For feature requests please go to Microsoft 365 Developer Platform Ideas Forum and see if your feature has already been requested or request a new feature there. Commented Jan 5, 2022 at 15:10
  • @chiz-ms: Thanks. Had posted it there early today. I was hoping for it not to be a "feature request" and that there was some possible workaround to achieve it, that's why I posted it here. Commented Jan 5, 2022 at 15:22
  • The Open Xml SDK can edit the xml parts as you describe, but it's in C# not JavaScript, so I don't know if that will solve your issue. Commented Jan 12, 2022 at 17:23
  • @CascadiaJS: Thanks. I know about OpenXML SDK. And yes, that's a C# thing. The desktop development tools for MS-Office (VBA, VSTO, OpenXML) are hugely more powerful and cover much more surface area than Office.js (even after 10 years of its development) and we still we see MS pushing all of us to move our add-ins to JS. Commented Jan 12, 2022 at 17:27
  • It's possible to view a w:pgSz with the following code 'const ooxml: OfficeExtension.ClientResult<string> = context.document.body.getOoxml();' Any idea why modifying this value does NOT work? Commented May 23 at 6:16

2 Answers 2

1

MS guys from Office.js team have told me that this is currently not possible with JS-based add-ins. What a pity. After 10 years of development, Office.js was supposed to be more powerful than this.

And yes, I have tried both the API as well as OOXML. None of them appears to work.

Sign up to request clarification or add additional context in comments.

Comments

0

Word / Office JS - Change page size of an open document

2 Comments

Please give context and explanation to your link. From How do I write a good answer: "Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the external resource is unreachable or goes permanently offline."
The page at this link simply says that it is not currently possible and that we can submit a "feature request" for it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.