I have a VBS script that takes an Excel file and saves it as a PDF. I call this vbs as within an RScript that is automated through Task Scheduler. The RScript runs just fine when I run it in R manually. However, when I have it scheduled in Task Scheduler, the task says it completes successfully, but the PDF file is not saved. What is different with Task Scheduler running the RScript to cause this to not actually complete?
Here is the RScript, which I have saved as HourlyR.R in the same C:\test folder.
shell.exec("C:/test/HourlyNew.vbs")
Here is the code for the HourlyNew.vbs
Option Explicit
ExcelMacro
Sub ExcelMacro()
Dim xlApp
Dim xlBook
Dim xlSheet
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\\test\\Hourly.xlsm", 0, False)
Set xlSheet = xlBook.Worksheets("HourlyDashboard")
xlApp.DisplayAlerts = False
xlApp.Visible = False
xlSheet.ExportAsFixedFormat 0, "c:\\test\\HourlyDashboard.pdf", 0, 1, 0, , , 0
xlBook.Close
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
Here is how I have my Scheduled task set up:
General Tab:
Run whether user is logged in or not selected
Run with highest privileges checked
Actions Tab:
Program/Script: C:\R_Program_Files\R-3.2.0\bin\x64\R.exe
Add arguments (optional): CMD BATCH C:\test\HourlyR.R