R: Create a New Word Instance

GetNewWrd {DescTools} | R Documentation

## Create a New Word Instance

### Description

Start a new instance of Word and return its handle. This handle allows controlling word afterwards.
WrdKill ends a running Word task.

### Usage

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

    WrdKill()

    createCOMReference(ref, className)

### 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
`ref` | the S object that is an external pointer containing the reference to the COM object
`className` | the name of the class that is "suggested" by the caller

### Details

RDCOMClient reveals the whole VBA-world of MS-Word. So generally speaking any VBA code can be run from R. 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:

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

new document | `wrd[["Documents"]]$Add(template, FALSE, 0)`, template is the templatename.
open | `wrd[["Documents"]]$Open(Filename="C:/MyPath/MyDocument.docx")`.
save | `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 1 | `WrdCaption("My Word-Story", stylename = wdConst$wdStyleHeading1)`
heading 2 | `WrdCaption("My Word-Story", stylename = wdConst$wdStyleHeading2)`
insert R output | `ToWrd(capture.output(str(d.diamonds)))`
pagebreak | `sel$InsertBreak(wdConst$wdPageBreak)`
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`
add bookmark | `wrd[["ActiveDocument"]][["Bookmarks"]]$Add("myBookmark")`
goto bookmark | `sel$GoTo(wdConst$wdGoToBookmark, 0, 0, "myBookmark")`
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="Abbildung")`

createCOMReference is just a wrapper for RDCOMClient::createCOMReference, as the function is not visible, if RDCOMClient is only used by required namespace.

### Value

a handle (pointer) to the created Word instance.

### Note

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

### Author(s)

Andri Signorell <andri@signorell.net>

### See Also

`GetNewXL`, `GetNewPP`

### Examples

    ## Not run:  # 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())
    }

    ## End(Not run)

[Package DescTools version 0.99.24 Index]