We are trying to automate the creation of some picture files within an R
Script.
We have the Excel files looking the way that we want
Consider having R do exactly as VBA does in your macro: making a COM interface to the Excel object library. You can do so with the RDCOMClient
package, retaining nearly same code as macro in the R syntax.
library(RDCOMClient)
xlApp <- COMCreate("Excel.Application")
xlWbk <- xlApp$Workbooks()$Open("C:\\Path\\To\\test_import.xlsx")
xlScreen = 1
xlBitmap = 2
xlWbk$Worksheets("deletedFields")$Range("A8:J36")$CopyPicture(xlScreen, xlBitmap)
xlApp[['DisplayAlerts']] <- FALSE
oCht <- xlApp[['Charts']]$Add()
oCht$Paste()
oCht$Export("C:\\Temp\\SavedRange.jpg", "JPG")
oCht$Delete()
# CLOSE WORKBOOK AND APP
xlWbk$Close(FALSE)
xlApp$Quit()
# RELEASE RESOURCES
oCht <- xlWbk <- xlApp <- NULL
rm(oCht, xlWbk, xlApp)
gc()
Output (random data/chart)