2

I'm creating a Word report using officer and flextable. Among others, I'm creating a flextable.

The problem is that after printing it into Word, while trying to open, the document is showing error: "there is an error with its contents" ("the file is corrupt"). Then I click OK and Word asks if to recover the contents. I click YES and the new document is being open with my data.

After removing flextable, Word is opening without error.

ft <- data.frame(c("data1", "data2"), c("text", variable1))  %>% 
  flextable() %>% theme_box() %>%
  delete_part(part = "header")

add_tab <- function(doc, placeholder, flextable, is_roman = FALSE) {
    doc %>% remove_placeholder(placeholder) %>%
        body_add_flextable(value = flextable, pos = "after") %>%
        body_add_par("")}

final_doc <- read_docx("C:/Users/Template.docx") %>% 
  add_tab(placeholder = "[table0]", flextable = ft) %>% 
  print("Output.docx")

How to avoid error masagges while opening the Word document with flextables?

Marta
  • 71
  • 8
  • 1
    I'm not conversant with r, but the error message you report means that the Word Open XML that has been generated is not valid - something's being put together incorrectly. You should use the [edit] link to provide a [mcve] - the minimal amount of code necessary to reproduce the problem (the code that uses flextable). It might also help to save the fixed document to a different file name, then compare its Open XML with that of the document generated by your code using the Open XML SKD Productivity Tool. That should give you some hints about what's not quite right... – Cindy Meister Jul 26 '19 at 12:21
  • You're going to need to include at least some of the code used to place the table into the document for folks to be able to help debug this – camille Jul 26 '19 at 13:58
  • Thanks for your comments, I've added the code. – Marta Jul 26 '19 at 20:09
  • 1
    I got to the point! When I changed Word template into Compatibility mode, it works perfectly. I have no idea what's that and how it works but maybe it will be useful info for someone else.. – Marta Jul 26 '19 at 21:15

0 Answers0