1

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.

user1934428
  • 19,864
  • 7
  • 42
  • 87
Vishal Sharma
  • 289
  • 2
  • 10
  • 1
    Please take a look at [this](https://stackoverflow.com/questions/32297699/hide-command-prompt-window-when-using-exec) very similar question. – Ben Mega Aug 13 '21 at 09:03
  • @VishalSharma : I took the liberty to add the _cmd_ tag, because _shell_ without specification of the kind of shell, implies POSIX shell. – user1934428 Aug 13 '21 at 09:16
  • @BenMega I have tried with that code and I am following it only. But the problem I am facing is in creating the command to pass to .Run 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""" I am not sure that I am creating the command correctly or not. I have taken the print of the command and runs it separately on cmd and it works fine but not with .Run – Vishal Sharma Aug 16 '21 at 06:10

0 Answers0