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")WrdKill()
a handle (pointer) to the created Word instance.
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
Andri Signorell <andri@signorell.net>
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(d.diamonds))) |
pagebreak | sel$InsertBreak(wdConst$wdPageBreak) |
sectionbreak | sel$InsertBreak(wdConst$wdSectionBreakContinuous) |
(wdSectionBreakNextPage ) | |
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(), |
Caption="Figure") | |
insert header | wview <- wrd[["ActiveWindow"]][["ActivePane"]][["View"]][["SeekView"]] |
wview <- ifelse(header, wdConst$wdSeekCurrentPageHeader, wdConst$wdSeekCurrentPageFooter) | |
ToWrd(x, ..., wrd=wrd) |
GetNewXL
, GetNewPP
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