1

I have made a simple console app that takes data entered into it, transforms the data into the correct format, and submits it to an online database. The console app just needs the data entered into it, and it will do the rest.

For example, I would type something like this: "Description text [Enter]", "Customer name [Enter]", etc., etc.

I have several Excel workbooks that I would like to use this app. But I don't know how to use VBA to do the following:

  1. Run the Console App
  2. Enter text into the console app from VBA for Excel.

How do I do this? Do I need to add additional references for Excel? Is this possible? If this isn't possible, what would you recommend doing instead?

Thank you in advance! This community has really helped so far.

0m3r
  • 12,286
  • 15
  • 35
  • 71
Parker.R
  • 88
  • 8
  • Can't help you with your problem directly, but generally it would be easier if your console app would accept the data as command line arguments, (`argv` if you're using C/C++). If you don't need a return value from the command, that should be very easy, i.e. see this question: https://stackoverflow.com/q/2290365/2192139 – Geier Nov 10 '18 at 21:49
  • I may look into how to do the argument function. Thank you, @Geier! – Parker.R Nov 11 '18 at 01:59

1 Answers1

1

Ideally, your console app would take arguments instead of reading lines. Then you could simply invoke your executable from VBA as explained here.

If you can't modify the console app, you can work around this by first creating a temporary text file using VBA (numerous examples throughout this site). This file would contain the inputs you'd normally provide from the keyboard. Then, you'd invoke your executable as described in the linked answer, but instead of providing parameters, you'd use the < character to feed the contents of the temporary text file to the program:

Public Sub StartExeWithInputFile()
    Dim strProgramName As String
    Dim strInputFile As String

    strProgramName = "C:\Program Files\Test\foobar.exe"
    strInputFile = "C:\Data\TempInputLines.txt" '<== You have to produce this file.

    Call Shell("""" & strProgramName & """ <""" & strInputFile & """", vbNormalFocus)
End Sub

EDIT Here's an example for creating the temporary input file mentioned above:

Sub CreateTempInputLines()
    Dim fso As Object
    Dim oFile As Object

    Set fso = CreateObject("Scripting.FileSystemObject")

    'See https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/createtextfile-method.
    Set oFile = fso.CreateTextFile("C:\Data\TempInputLines.txt", True)

    oFile.WriteLine "Description text"
    oFile.WriteLine "Customer name"

    'Cleanup.
    oFile.Close
    Set oFile = Nothing
    Set fso = Nothing
End Sub
Excelosaurus
  • 2,789
  • 1
  • 14
  • 20
  • Great answer. The app opens perfectly, and the text is written correctly. Everything works _except_ the input into the console app; it is just blank. I can even copy the contents of the .txt file, and it works. Any idea what it might be? – Parker.R Nov 11 '18 at 01:58
  • Maybe you started the console app before closing the text file in VBA? The data might not be written to the text file until you close it. – Geier Nov 11 '18 at 07:22
  • Put a breakpoint on the Shell line and Debug.Print what's within the parentheses. Copy the result and paste it into a command window. What happens? Note: I've built a console app that just calls Console.ReadLine(), and it works. – Excelosaurus Nov 11 '18 at 19:52
  • Edited my answer to align the text file's path across subs; might explain why it did not work. Of course, make sure you create, write to and close the text file *before* calling Shell. – Excelosaurus Nov 12 '18 at 02:33