I am trying to call the R script from the VBA code and I am using VBA.CreateObject("WScript.Shell").Exec
which shows the pop up of the shell window and then it closes. My output is working fine but I don't want any window popup which can be achieved using VBA.CreateObject("WScript.Shell").Run
command. The problem here is that, I am not able to convert my command so that it is executed with VBA.CreateObject("WScript.Shell").Run
. The idea with .Run
is to print the output in an output file and then read the output file.
'Function to call the Rscript to convert any case to title case
Public Function ToTitleCase(Text As String) As String
Dim shell As Object
Set shell = VBA.CreateObject("WScript.Shell")
Dim waitTillComplete As Boolean: waitTillComplete = True
Dim path As String
path = """C:\Program Files\R\R-4.0.5\bin\Rscript.exe"" ""S:\01) Analytics\Temp\AnalystInput\ConvertToTitleCase.R"" -t " + """" + Text + """"
ToTitleCase = shell.Exec(path).StdOut.ReadAll
End Function
This code is working fine. The code I want to do is below:
'Function to call the Rscript to convert any case to title case
Public Function ToTitleCase(Text As String) As String
Dim shell As Object
Set shell = VBA.CreateObject("WScript.Shell")
Dim waitTillComplete As Boolean: waitTillComplete = True
Dim path As String
'path = """C:\Program Files\R\R-4.0.5\bin\Rscript.exe"" ""S:\01) Analytics\Temp\AnalystInput\ConvertToTitleCase.R"" -t " + """" + Text + """"
'ToTitleCase = shell.Exec(path).StdOut.ReadAll
path = """C:\Program Files\R\R-4.0.5\bin\Rscript.exe"" ""S:\01) Analytics\Temp\AnalystInput\ConvertToTitleCase.R"" -t " + """" + Text + """" + " > " + """C:\Users\AnalystInput\output.txt"""
Debug.Print path
With CreateObject("WScript.Shell")
' Pass 0 as the second parameter to hide the window...
.Run path, 0, True
End With
' Read the output and remove the file when done...
Dim strOutput
With CreateObject("Scripting.FileSystemObject")
strOutput = .OpenTextFile("C:\Users\AnalystInput\output.txt").ReadAll()
'.DeleteFile "c:\out.txt"
End With
End Function
I have taken the print of the path
and it comes out "C:\Program Files\R\R-4.0.5\bin\Rscript.exe" "S:\01) Analytics\Temp\AnalystInput\ConvertToTitleCase.R" -t "VishalKumar" > "C:\Users\AnalystInput\output.txt"
which works fine in windows command prompt.
Can any please help me here.