Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Field codes using putdocx

    I use putdocx to create standardized outputs for my statistical analyses. I am writing an own command to create a "table of contents" in the beginning of the document. The table of contents is actually a table where each row contains the text of a caption, as a cross-reference link to the corresponding paragraph (using bookmarks). Rows are added flexibly when a new caption is added to the document.

    Besides linking the captions to the paragraphs, I would like to add page numbers to the TOC as well, so that it is easier to read the file when reading in printed form. However, I am struggling to find any solution for that other than hard-coding the page number in my do-files.

    I found that field codes for Word documents might be the solution, specifically the "PAGEREF bookmark" field code (see https://support.microsoft.com/en-US/...-pageref-field). However, I don't know how to insert a field code into a table cell (or as regular text) using Stata.
    From my docx outputs I know that Stata adds page numbers in the footer as field codes, using some Mata commands. However I am not familiar with Mata coding so I cannot follow what's happening in the putdocx code and I cannot copy/alter it for my purposes.

    Can anybody help me out with a solution?

  • #2
    Stata does support inserting automatic page numbers (-help putdocx pagenumber-) if that is of help.

    In a broader sense for field codes more generally, you can search the forums for any hints. When I last went down this rabbit hole, StataCorp only supports a limited subset of field codes (e.g., those for pagination) through direct commands. Under the hood, Stata works with the open-standard version of docx, called Office Open XML. Microsoft maintains it's own proprietary version of this XML-style container for docx and xlsx files (Microsoft Office XML), but sponsors the open standard version in parallel. I don't know enough about the differences between OOXML and MSXML formats but this may well be one major difference.

    A really cumbersome approach IMHO for anyone wanting to undertake this kind of task would involve creating multiple docx files and merging them together later. For example, manually make a 1 page docx that contains the desired table of contents and formatting, and then in Stata, build the contents of the document, then merge them altogether. if you have appropriately defined style headings and such in advance, then the stitched together document should have a TOC which appropriately links to the listed contents. For my own needs, I rarely need to programmatically makes these documents anymore, so if I needed anything specific I would add (minimal) formatting at the end.

    Comment


    • #3
      Thank You! Sadly, automatic page numbers by putdocx pagenumber does not help, as it can only be used to add the number (as a field ode) to the footer. But there is no way to read its value, as the value is only determined when the docx is opened in an editor. I did not find any other way to read the current page number of the docx under production either.

      Comment


      • #4
        I'm not aware of any method (Stata or otherwise) that can read the result of a field code outside of MS Word itself.

        Comment

        Working...
        X