1

How do you redirect input to an executable from inside VBA? Specifically, why does the code below not work?

ChDir theRightDirectory
Set WshShell = VBA.CreateObject("WScript.Shell") 
WshShell.Run "runme < start.txt", 1, True

Or

RetVal = Shell("runme < start.txt", vbNormalFocus)  

runme.exe does start up all right, but the input is not redirected and has to be type in manually in the command window. Also tried:

RetVal = Shell("type start.txt | runme.exe", vbNormalFocus)

Piping the output of type start.txt into runme.exe just plain returns a “file not found” error.

Yet when I type those various commands directly at the command line, they all work.

3
  • Do you run anything else at the command line before you execute the commands that include the | or the <?
    – Raj More
    Dec 7, 2009 at 15:10
  • No, nothing else is run before, or after. Dec 7, 2009 at 15:20
  • Do you get the same result when you specify the full path for executable and the parameter?
    – Raj More
    Dec 7, 2009 at 15:39

4 Answers 4

4

Execute the command this way:

WshShell.Run "%COMSPEC% /C runme < start.txt", 1, True

That way it will run it through the command line interpreter.

0
3

I've been made aware of this solution: No need to write the input to start.txt; input can just be fed directly to the input stream. (My question should have made clear that this is also an option.) A bonus is that the user can get feedback from the output stream.

Set WshShell = VBA.CreateObject("WScript.Shell")
WshShell.CurrentDirectory = theRightDirectory
Set oExec = WshShell.exec("runme.exe")

' Write to the input stream
oExec.StdIn.Write "some input for the first prompt" & vbCrLf & _
                  "some more input" & vbCrLf 

' The output stream can be shown to the user, e.g.
sOutput = oExec.StdOut.ReadAll()
MsgBox (sOutput)
2

I'll answer a more general part of your question. You asked:

Specifically, why does the code below not work?

WshShell.Run "runme < start.txt", 1, True

The reason the code does not work as you expect is because the shell of WScript.Shell is not the same as the shell of cmd.exe. In particular, it does not do redirects or pipes.

If you were able to debug the runme process you would see that it has been passed two arguments, < and start.txt

As previously answered, there are two ways to fix this:

  • feed the WScript.Shell StdIn directly and remove your redirect

    set oExec = WshShell.exec(cmdLine)
    oExec.StdIn.Write "some input for the first prompt" & vbCrLf

  • run the DOS shell instead of runme, let it interpret the rest of the commandline, including the redirect

    WshShell.Run "%COMSPEC% /C runme < start.txt", 1, True

2
  • This answer is 5½ years late but the extra explanation actually adds value to the existing answers! Though note, the intent was to pipe things in, i.e. equivalent to StdIn, not StdOut, as should be apparent from my own answer to this question. Jul 3, 2015 at 7:30
  • Thanks for the support, and well spotted. I've plucked the wrong line from your own answer, should have used the StdIn.Write instead. Will edit my answer. Jul 3, 2015 at 8:35
0

You are probably shelling out to a different folder than you expect.

You may have to change directory using CHDIR to the appropriate folder, or qualify your file names using the entire or relative path.

1
  • I do use ChDir to set the appropriate directory. runme.exe and start.txt are both in the same folder. Edited question to reflect this. Dec 7, 2009 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.