Generated docx in Word 365 - PAGEREF fields not working

Thomas Hunkapiller 1 Reputation point
2021-10-27T00:15:10.473+00:00

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 Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,489 questions
Word Management
Word Management
Word: A family of Microsoft word processing software products for creating web, email, and print documents.Management: The act or process of organizing, handling, directing or controlling something.
893 questions
{count} votes