Search code examples
rms-wordofficer

officer: this document contains fields that may refer to other files


I'm creating Microsoft Word outputs using the amazing officer package in R. I'm using a template Word document to specify much of the formatting.

Below is a code snippet that illustrates what I'm doing.

library("officer")

read_docx("Output Template Blank.docx") %>%
body_add_par(value = "Kaplan-Meier Analysis of Time (Months) to HSV-2 Episodes", style = "caption") %>%
body_add_flextable(my_km_table, align = "left") %>%
print("Kaplan-Meier Output.docx")

This generally works very well. The only trouble is that opening the document generates an unwanted message in Word.

"This document contains fields that may refer to other files. Do you want to update the fields in the document?"

I can get rid of this by clicking "Yes," slightly altering the document by adding a space, and then hitting save. I'd prefer not to have to do this manually though and was hoping there is a better way. I investigated this some time back and recall that there is a way to turn this off in Microsoft Word. I also recall that this was seen as something of a security risk. I'm not sure how much of a risk. So am wondering if this could be a good solution or if there truly is a better way.


Solution

  • This is a deliberate design decision for security reasons. Some kinds of Word fields can access external data. Microsoft's policy on this point is that responsibility for opening a document (and taking a risk) lies with the user - the user needs to decide whether the document comes from a trusted source.

    For this reason, if the fields are set to automatically update, a message will be displayed asking the user whether to allow the update.

    It's possible to insert fields and not set the automatic update. In this case, the user will need to manually update the fields or there could be an add-in that takes care of this when any document is opened. Since the user will have made the choice to install the add-in, that's again the user's responsibility.

    The only other way to suppress the message is to have opened the document and updated the fields before passing it to the user. Programmatically, this could be done either using Word automation (not server-side) or in an on-premises version of SharePoint that has the Word Automation Services installed.