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
You can do this with vbs. Most vbs is identical to vba so you can write out your dynamic vbs script which includes your macro as text and then call it with shell.
Here is a working example:
fileConn<-file("c:/rworking/test/test.vbs")
writeLines(c("Dim xlApp, xlBook, xlSht",
"Dim filename",
"filename = \"c:\\Rworking\\test\\test_import.xlsx\"",
"Set xlApp = CreateObject(\"Excel.Application\")",
"xlApp.Visible = True",
"set xlBook = xlApp.WorkBooks.Open(filename)",
"set xlSht = xlApp.Worksheets(1)",
"set rng = xlSht.Range(\"A8:J36\")",
"rng.CopyPicture",
"Set oCht = xlApp.Charts",
"oCht.Add() ",
"Set oCht = oCht(1)",
"oCht.paste",
"oCht.Export \"C:\\rworking\\test\\Test.jpg\", \"JPG\""),
fileConn)
close(fileConn)
shell.exec("c:/rworking/test/test.vbs")