question

devoidfury avatar image
0 Votes"
devoidfury asked devoidfury commented

Generated docx in Word 365 - PAGEREF fields not working

I'm generating a docx from code, and trying to implement my own sort of "Table of Contents" because it's a long document and we have specific needs on what to show and what to omit in the ToC.

The issue I'm running into is that PAGEREF fields don't seem to work at all in 365 word, and only show "1" in desktop word, even when Automatically Update Fields setting is turned on (w:updateFields).

Any ideas on how to go about this via plain OOXML? I need to get the page numbers in there on my custom table of contents.

Here's how I'm using the PAGEREF field:

 <w:p>
   <w:pPr>
     <w:pStyle w:val="ListParagraph"/>
     <w:numPr>
       <w:ilvl w:val="0"/>
       <w:numId w:val="1"/>
     </w:numPr>
     <w:tabs>
       <w:tab w:val="right" w:pos="9026" w:leader="dot"/>
     </w:tabs>
     <w:jc w:val="left"/>
   </w:pPr>
   <w:hyperlink w:history="1" w:anchor="Page1">
     <w:r>
       <w:t xml:space="preserve">Page Name</w:t>
     </w:r>
   </w:hyperlink>
   <w:r>
     <w:t xml:space="preserve">  </w:t>
   </w:r>
   <w:r>
     <w:fldChar w:fldCharType="begin" w:dirty="true"/>
     <w:instrText xml:space="preserve">PAGEREF Page1</w:instrText>
     <w:fldChar w:fldCharType="end"/>
   </w:r>
 </w:p>


And the bookmark it's pointing to, on another page:

 <w:p>
   <w:pPr>
     <w:pStyle w:val="Heading1"/>
     <w:jc w:val="left"/>
   </w:pPr>
   <w:bookmarkStart w:name="Page1" w:id="1"/>
   <w:r>
     <w:t xml:space="preserve">Page Name</w:t>
   </w:r>
   <w:bookmarkEnd w:id="1"/>
 </w:p>


I also tried with a simple field, it has the same issue, the field is blank in word365 and doesn't calculate the page number:

 <w:p>
   <w:pPr>
     <w:pStyle w:val="ListParagraph"/>
     <w:numPr>
       <w:ilvl w:val="0"/>
       <w:numId w:val="1"/>
     </w:numPr>
     <w:tabs>
       <w:tab w:val="right" w:pos="9026" w:leader="dot"/>
     </w:tabs>
     <w:jc w:val="left"/>
   </w:pPr>
   <w:hyperlink w:history="1" w:anchor="Page1">
     <w:r>
       <w:t xml:space="preserve">Page Name</w:t>
     </w:r>
   </w:hyperlink>
   <w:r>
     <w:t xml:space="preserve">  </w:t>
   </w:r>
   <w:fldSimple w:instr=" PAGEREF Page1 "/>
 </w:p>
office-addins-devoffice-word-itpro
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@devoidfury
Welome to Q&A forum ~
But please note, tag "office-word-itpro" means general issues of Word client, your issue is more focused on Open XML Format SDK.
To better help you, I would add a tag of XML.
Thanks for your understanding.

0 Votes 0 ·

0 Answers