rms-wordflextableofficer

How can I avoid the error "This document contains fields that may refer to other files." when adding page numbers to the header using officer in R?


I'm using the officer package in R to create tables and figures that output to a Word document. I want to a "Page X of N" field in the header. The following code does what I want it to.

library(officer)
library(flextable)

doc = read_docx()

my_header = flextable(data = data.frame(V1 = c("Protocol", "Compound"),
                                        V2 = c("Company" ,"Page"))) |>
  delete_part(part = "header") |>
  theme_booktabs() |>
  border_remove() |>
  align(j = 1, align = "left", part = "body") |>
  align(j = 2, align = "right", part = "body") |>
  compose(part="body",i=2,j=2,
          as_paragraph("Page ",
                       as_word_field(x="Page"),
                       " of ",
                       as_word_field(x="NumPages"))) |>
  width(width = c(4.5, 4.5))

doc = body_set_default_section(doc,
                               value = prop_section(page_size = 
page_size(orient="landscape",width = 8.5,height=11),
                                                header_default = block_list(my_header)))

print(doc, target = "test.docx")

This produces the following:

Officer Output

The problem is that when I open the document, I always get this message:

enter image description here

Is there a way to add the "Page X of N" without getting this message?


Solution

  • If you have Word installed, you can run/use package 'doconv'.

    library(doconv)
    print(doc, target = "test.docx") |> docx_update()
    

    See https://github.com/ardata-fr/doconv#update-word-fields-and-toc