Learn R Programming

DescTools (version 0.99.52)

GetNewWrd: Create a New Word Instance

Description

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.

Usage

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

WrdKill()

Value

a handle (pointer) to the created Word instance.

Arguments

visible

logical, should Word made visible? Defaults to TRUE.

template

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

header

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

main

the main title of the report

Author

Andri Signorell <andri@signorell.net>

Details

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 documentwrd[["Documents"]]$Add(template, FALSE, 0), template is the templatename.
open documentwrd[["Documents"]]$Open(Filename="C:/MyPath/MyDocument.docx").
save documentwrd$ActiveDocument()$SaveAs2(FileName="P:/MyFile.docx")
quit wordwrd$quit()
kill word taskWrdKill kills a running word task (which might not be ended with quit.)
normal textUse 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 textsel$TypeText("sed diam nonumy eirmod tempor invidunt ut labore")
headingWrdCaption("My Word-Story", index=1)
insert R outputToWrd(capture.output(str(d.diamonds)))
pagebreaksel$InsertBreak(wdConst$wdPageBreak)
sectionbreaksel$InsertBreak(wdConst$wdSectionBreakContinuous)
(wdSectionBreakNextPage)
move cursor rightsel$MoveRight(Unit=wdConst$wdCharacter, Count=2, Extend=wdConst$wdExtend)
goto endsel$EndKey(Unit=wdConst$wdStory)
pagesetupsel[["PageSetup"]][["Bottommargin"]] <- 4 * 72
orientationsel[["PageSetup"]][["Orientation"]] <- wdConst$wdOrientLandscape
add bookmarkwrd[["ActiveDocument"]][["Bookmarks"]]$Add("myBookmark")
goto bookmarksel$GoTo(wdConst$wdGoToBookmark, 0, 0, "myBookmark")
update bookmarkWrdUpdateBookmark("myBookmark", "New text for my bookmark")
show document map wrd[["ActiveWindow"]][["DocumentMap"]] <- TRUE
create tableWrdTable() which allows to define the table's geometry
insert captionsel$InsertCaption(Label="Abbildung", TitleAutoText="InsertCaption",
Title="My Title")
tables of figureswrd$ActiveDocument()$TablesOfFigures()$Add(Range=sel$range(),
Caption="Figure")
insert headerwview <- wrd[["ActiveWindow"]][["ActivePane"]][["View"]][["SeekView"]]
wview <- ifelse(header, wdConst$wdSeekCurrentPageHeader, wdConst$wdSeekCurrentPageFooter)
ToWrd(x, ..., wrd=wrd)

See Also

GetNewXL, GetNewPP

Examples

Run this code
if (FALSE)  # Windows-specific example

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

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

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

Run the code above in your browser using DataLab