GetNewWrd {DescTools}R Documentation

Create a New Word Instance


Start a new instance of Word and return its handle. By means of this handle we can then control the word application.
WrdKill ends a running MS-Word task.


GetNewWrd(visible = TRUE, template = "Normal", header = FALSE,
          main = "Descriptive report")




logical, should Word made visible? Defaults to TRUE.


the name of the template to be used for creating a new document.


logical, should a caption and a list of contents be inserted? Default is FALSE.


the main title of the report


The package RDCOMClient reveals the whole VBA-world of MS-Word. So generally speaking any VBA code can be run fully controlled by R. In practise, it might be a good idea to record a macro and rewrite the VB-code in R.

Here's a list of some frequently used commands. Let's assume we have a handle to the application and a handle to the current selection defined as:

wrd <- GetNewWrd()
sel <- wrd$Selection()

Then we can access the most common properties as follows:

new document wrd[["Documents"]]$Add(template, FALSE, 0), template is the templatename.
open document wrd[["Documents"]]$Open(Filename="C:/MyPath/MyDocument.docx").
save document wrd$ActiveDocument()$SaveAs2(FileName="P:/MyFile.docx")
quit word wrd$quit()
kill word task WrdKill kills a running word task (which might not be ended with quit.)
normal text Use ToWrd which offers many arguments as fontname, size, color, alignment etc.
ToWrd("Lorem ipsum dolor sit amet, consetetur",
font=list(name="Arial", size=10, col=wdConst$wdColorRed)
simple text sel$TypeText("sed diam nonumy eirmod tempor invidunt ut labore")
heading WrdCaption("My Word-Story", index=1)
insert R output ToWrd(capture.output(str(
pagebreak sel$InsertBreak(wdConst$wdPageBreak)
sectionbreak sel$InsertBreak(wdConst$wdSectionBreakContinuous)
move cursor right sel$MoveRight(Unit=wdConst$wdCharacter, Count=2, Extend=wdConst$wdExtend)
goto end sel$EndKey(Unit=wdConst$wdStory)
pagesetup sel[["PageSetup"]][["Bottommargin"]] <- 4 * 72
orientation sel[["PageSetup"]][["Orientation"]] <- wdConst$wdOrientLandscape
add bookmark wrd[["ActiveDocument"]][["Bookmarks"]]$Add("myBookmark")
goto bookmark sel$GoTo(wdConst$wdGoToBookmark, 0, 0, "myBookmark")
update bookmark WrdUpdateBookmark("myBookmark", "New text for my bookmark")
show document map wrd[["ActiveWindow"]][["DocumentMap"]] <- TRUE
create table WrdTable() which allows to define the table's geometry
insert caption sel$InsertCaption(Label="Abbildung", TitleAutoText="InsertCaption",
Title="My Title")
tables of figures wrd$ActiveDocument()$TablesOfFigures()$Add(Range=sel$range(),
insert header wview <- wrd[["ActiveWindow"]][["ActivePane"]][["View"]][["SeekView"]]
wview <- ifelse(header, wdConst$wdSeekCurrentPageHeader, wdConst$wdSeekCurrentPageFooter)
ToWrd(x, ..., wrd=wrd)


a handle (pointer) to the created Word instance.


Note that the list of contents has to be refreshed by hand after inserting text (if inserted by header = TRUE).


Andri Signorell <>

See Also

GetNewXL, GetNewPP


## Not run:  # Windows-specific example

wrd <- GetNewWrd()
Desc([,1:4], wrd=wrd)

wrd <- GetNewWrd(header=TRUE)
Desc([,1:4], wrd=wrd)

# enumerate all bookmarks in active document
for(i in 1:wrd[["ActiveDocument"]][["Bookmarks"]]$count()){

## End(Not run)

[Package DescTools version 0.99.54 Index]