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()

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

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)

Note

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

Value

a handle (pointer) to the created Word instance.

Author

Andri Signorell <andri@signorell.net>

See also

Examples

if (FALSE)  # Windows-specific example

wrd <- GetNewWrd()
Desc(d.pizza[,1:4], wrd=wrd)
#> Error: object 'wrd' not found

wrd <- GetNewWrd(header=TRUE)
#> Loading required namespace: RDCOMClient
#> Warning: RDCOMClient is unfortunately not available for Linux systems (Windows-only).
Desc(d.pizza[,1:4], wrd=wrd)
#> ────────────────────────────────────────────────────────────────────────────── 
#> Describe d.pizza[, 1:4] (data.frame):
#> 
#> data frame:	1209 obs. of  4 variables
#> 		1177 complete cases (97.4%)
#> 
#>   Nr  Class  ColName  NAs        Levels
#>   1   int    index     .               
#>   2   dat    date     32 (2.6%)        
#>   3   num    week     32 (2.6%)        
#>   4   num    weekday  32 (2.6%)        
#> 
#> 
#> ────────────────────────────────────────────────────────────────────────────── 
#> 1 - index (integer)
#> 
#>     length       n     NAs  unique      0s      mean    meanCI'
#>      1'209   1'209       0     = n       0    605.00    585.30
#>             100.0%    0.0%            0.0%              624.70
#>                                                               
#>        .05     .10     .25  median     .75       .90       .95
#>      61.40  121.80  303.00  605.00  907.00  1'088.20  1'148.60
#>                                                               
#>      range      sd   vcoef     mad     IQR      skew      kurt
#>   1'208.00  349.15    0.58  447.75  604.00      0.00     -1.20
#>                                                               
#> lowest : 1, 2, 3, 4, 5
#> highest: 1'205, 1'206, 1'207, 1'208, 1'209
#> 
#> ' 95%-CI (classic)
#> 

#> ────────────────────────────────────────────────────────────────────────────── 
#> 2 - date (Date)
#> 
#>   length      n    NAs unique
#>    1'209  1'177     32     31
#>           97.4%   2.6%       
#> 
#> lowest : 2014-03-01 (42), 2014-03-02 (46), 2014-03-03 (26), 2014-03-04 (19)
#> highest: 2014-03-28 (46), 2014-03-29 (53), 2014-03-30 (43), 2014-03-31 (34)
#> 
#> 
#> Weekday:
#> 
#> Pearson's Chi-squared test (1-dim uniform):
#>   X-squared = 78.879, df = 6, p-value = 6.09e-15
#> 
#>        level  freq   perc  cumfreq  cumperc
#> 1     Monday   144  12.2%      144    12.2%
#> 2    Tuesday   117   9.9%      261    22.2%
#> 3  Wednesday   134  11.4%      395    33.6%
#> 4   Thursday   147  12.5%      542    46.0%
#> 5     Friday   171  14.5%      713    60.6%
#> 6   Saturday   244  20.7%      957    81.3%
#> 7     Sunday   220  18.7%    1'177   100.0%
#> 
#> Months:
#> 
#> Pearson's Chi-squared test (1-dim uniform):
#>   X-squared = 12947, df = 11, p-value < 2.2e-16
#> 
#>         level   freq    perc  cumfreq  cumperc
#> 1     January      0    0.0%        0     0.0%
#> 2    February      0    0.0%        0     0.0%
#> 3       March  1'177  100.0%    1'177   100.0%
#> 4       April      0    0.0%    1'177   100.0%
#> 5         May      0    0.0%    1'177   100.0%
#> 6        June      0    0.0%    1'177   100.0%
#> 7        July      0    0.0%    1'177   100.0%
#> 8      August      0    0.0%    1'177   100.0%
#> 9   September      0    0.0%    1'177   100.0%
#> 10    October      0    0.0%    1'177   100.0%
#> 11   November      0    0.0%    1'177   100.0%
#> 12   December      0    0.0%    1'177   100.0%
#> 
#> By days :
#> 
#>          level  freq  perc  cumfreq  cumperc
#> 1   2014-03-01    42  3.6%       42     3.6%
#> 2   2014-03-02    46  3.9%       88     7.5%
#> 3   2014-03-03    26  2.2%      114     9.7%
#> 4   2014-03-04    19  1.6%      133    11.3%
#> 5   2014-03-05    33  2.8%      166    14.1%
#> 6   2014-03-06    39  3.3%      205    17.4%
#> 7   2014-03-07    44  3.7%      249    21.2%
#> 8   2014-03-08    55  4.7%      304    25.8%
#> 9   2014-03-09    42  3.6%      346    29.4%
#> 10  2014-03-10    26  2.2%      372    31.6%
#> 11  2014-03-11    34  2.9%      406    34.5%
#> 12  2014-03-12    36  3.1%      442    37.6%
#> 13  2014-03-13    35  3.0%      477    40.5%
#> 14  2014-03-14    38  3.2%      515    43.8%
#> 15  2014-03-15    48  4.1%      563    47.8%
#> 16  2014-03-16    47  4.0%      610    51.8%
#> 17  2014-03-17    30  2.5%      640    54.4%
#> 18  2014-03-18    32  2.7%      672    57.1%
#> 19  2014-03-19    31  2.6%      703    59.7%
#> 20  2014-03-20    36  3.1%      739    62.8%
#> 21  2014-03-21    43  3.7%      782    66.4%
#> 22  2014-03-22    46  3.9%      828    70.3%
#> 23  2014-03-23    42  3.6%      870    73.9%
#> 24  2014-03-24    28  2.4%      898    76.3%
#> 25  2014-03-25    32  2.7%      930    79.0%
#> 26  2014-03-26    34  2.9%      964    81.9%
#> 27  2014-03-27    37  3.1%    1'001    85.0%
#> 28  2014-03-28    46  3.9%    1'047    89.0%
#> 29  2014-03-29    53  4.5%    1'100    93.5%
#> 30  2014-03-30    43  3.7%    1'143    97.1%
#> 31  2014-03-31    34  2.9%    1'177   100.0%
#> 



#> ────────────────────────────────────────────────────────────────────────────── 
#> 3 - week (numeric)
#> 
#>   length      n    NAs  unique     0s   mean  meanCI'
#>    1'209  1'177     32       6      0  11.40   11.33
#>           97.4%   2.6%           0.0%          11.48
#>                                                     
#>      .05    .10    .25  median    .75    .90     .95
#>     9.00  10.00  10.00   11.00  13.00  13.00   13.00
#>                                                     
#>    range     sd  vcoef     mad    IQR   skew    kurt
#>     5.00   1.33   0.12    1.48   3.00  -0.07   -1.01
#>                                                     
#> 
#>    value  freq   perc  cumfreq  cumperc
#> 1      9    88   7.5%       88     7.5%
#> 2     10   258  21.9%      346    29.4%
#> 3     11   264  22.4%      610    51.8%
#> 4     12   260  22.1%      870    73.9%
#> 5     13   273  23.2%    1'143    97.1%
#> 6     14    34   2.9%    1'177   100.0%
#> 
#> ' 95%-CI (classic)
#> 

#> ────────────────────────────────────────────────────────────────────────────── 
#> 4 - weekday (numeric)
#> 
#>   length      n    NAs  unique    0s   mean  meanCI'
#>    1'209  1'177     32       7     0   4.44    4.33
#>           97.4%   2.6%          0.0%           4.56
#>                                                    
#>      .05    .10    .25  median   .75    .90     .95
#>     1.00   1.00   3.00    5.00  6.00   7.00    7.00
#>                                                    
#>    range     sd  vcoef     mad   IQR   skew    kurt
#>     6.00   2.02   0.45    2.97  3.00  -0.34   -1.17
#>                                                    
#> 
#>    value  freq   perc  cumfreq  cumperc
#> 1      1   144  12.2%      144    12.2%
#> 2      2   117   9.9%      261    22.2%
#> 3      3   134  11.4%      395    33.6%
#> 4      4   147  12.5%      542    46.0%
#> 5      5   171  14.5%      713    60.6%
#> 6      6   244  20.7%      957    81.3%
#> 7      7   220  18.7%    1'177   100.0%
#> 
#> ' 95%-CI (classic)
#> 


# enumerate all bookmarks in active document
for(i in 1:wrd[["ActiveDocument"]][["Bookmarks"]]$count()){
  print(wrd[["ActiveDocument"]][["Bookmarks"]]$Item(i)$Name())
}
#> Error: attempt to apply non-function
 # \dontrun{}