2

I am trying change the properties of a word document with the officer library and I am having problems to succeed.

I need to change the margins of the entire document.

Let's say I have this document:

barometer <- read_docx %>%
  body_add_par("A title", style = "heading 1") %>%   
  body_add_par("Hello world!", style = "Normal") %>%
  print(target = "test.docx")

Does any one knowns to set the margins of the document?

Thanks!

David Gohel
  • 9,180
  • 2
  • 16
  • 34
Bruno Guarita
  • 767
  • 10
  • 21
  • In cases like this, I usually open Word, open the Developer ribbon tab, hit "record macro" , and then grab whatever buttons do what I want done -- in your case, changing the margins-- then look at the resultant VBS commands. – Carl Witthoft May 22 '20 at 15:40

1 Answers1

2

Do it the correct and easy way. First, open any word document and change everything to be exactly as you want it: font, indents, margins, etc. Then save that layout as a new Style. Then over in officeR all you need to do is specify that style rather than "Normal."

If something like page margins aren't included in the Style (I forget at the moment), then go one step farther, again in Word. Do everything I listed in the first paragraph. Make sure there's no text or pictures or whatever in the document (completely blank). Then SaveAs and select ".dot" or ".dotx" to save as a document template. Now when you run officeR you can specify that template and a new ".docx" file will be created. You can name it and save it with officeR commands.

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73