Shell Command With Parameters

atr140

Board Regular
Joined
Nov 27, 2012
Messages
72
I am trying to run a program from excel using shell; however, the program will execute but seems to be failing and closing after the first parameter is passed.

Test code:

VBA Code:
Sub Run_BXLRFDtst()
    Dim Dummy As Double
    Dim BXLRFD_Input_File_Name As String
    Dim BXLRFD_Output_File_Name As String

    Dim BXLRFD_Location As String

    BXLRFD_Location = "\\files\trans\__Standards-Examples-Templates\DP Design Programs\50\15 PS 3 (LFD)\PS3 v3.6.0.3\PS3.exe"

    BXLRFD_Input_File_Name = "C:\Users\arovenolt\Desktop\t\TEST.txt"
    BXLRFD_Output_File_Name = "C:\Users\arovenolt\Desktop\t\TEST_IN.OUT"
    
    Dummy = Shell(Chr(34) & BXLRFD_Location & Chr(34) & " " & Chr(34) & BXLRFD_Input_File_Name & Chr(34) & " " & "N" & " " & "N" & " " & Chr(34) & BXLRFD_Output_File_Name & Chr(34), 1)        

End Sub

In research, I realized there may be an issue passing file paths with spaces so I wrapped all variables with Chr(34) as advised in other threads.

Also of note, if I only execute the program using shell, I can manually enter the other information and the program functions fine.

Test Code:
VBA Code:
Sub Run_BXLRFDtst()
    Dim Dummy As Double
    Dim BXLRFD_Input_File_Name As String
    Dim BXLRFD_Output_File_Name As String

    Dim BXLRFD_Location As String

    BXLRFD_Location = "\\files\trans\__Standards-Examples-Templates\DP Design Programs\50\15 PS 3 (LFD)\PS3 v3.6.0.3\PS3.exe"

    BXLRFD_Input_File_Name = "C:\Users\arovenolt\Desktop\t\TEST.txt"
    BXLRFD_Output_File_Name = "C:\Users\arovenolt\Desktop\t\TEST_IN.OUT"
    
    Dummy = Shell(Chr(34) & BXLRFD_Location & Chr(34) , 1)        

End Sub

Any help would be appreciated!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I'm not at all sure this will work, but this is the approach that worked for me with VBScript code when calling a DOS batch file which took arguments (well, just about. It seems that I needed to create a shell object here to get it to at least start to work).
VBA Code:
Sub Test_Shell()

    Dim Dummy As Double
    Dim BXLRFD_Input_File_Name As String
    Dim BXLRFD_Output_File_Name As String

    Dim BXLRFD_Location As String

    BXLRFD_Location = "\\files\trans\__Standards-Examples-Templates\DP Design Programs\50\15 PS 3 (LFD)\PS3 v3.6.0.3\"

    BXLRFD_Input_File_Name = "C:\Users\arovenolt\Desktop\t\TEST.txt"
    BXLRFD_Output_File_Name = "C:\Users\arovenolt\Desktop\t\TEST_IN.OUT"


'-------------New Code-------------------
On Error GoTo Restore_Directory 'Uncomment when trying to debug!  But here because it will potentially through off Windows directory temporarily if not done!
 
    With CreateObject("WScript.Shell")
        Dim s As String
        s = Chr(34) & " " & Chr(34)
        Dim directoryBefore As String
        directoryBefore = .CurrentDirectory
        .CurrentDirectory = BXLRFD_Location
        .Run "PS3.exe" & " " & Chr(34) & BXLRFD_Input_File_Name & s & "N" & s & "N" & s & BXLRFD_Output_File_Name & Chr(34)
Restore_Directory:
        .CurrentDirectory = directoryBefore
    End With

End Sub

Regarding the beginning of BXLRFD_Location, I'm not familiar with \\ in VBScript (or VBA) (I know that's required in JavaScript, for example.) So for my VBScript, I literally just used:
VBA Code:
 BXLRFD = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
to get the parent folder path of the EXE that I was going to call the batch file when the user clicked a button on it . . . and I had the batch file in a subfolder of the same parent folder. Maybe you will need to manually put in that folder path (the immediate parent) in for it (so that it starts with the Drive's letter). But it appears that it is what you have (obviously). But I don't know if it will recognize the \\. Not sure if this is a shared drive, etc.
 
Upvote 0
Thanks for the reply. I tested the code with the same results. It still seems the Input File Name is not "reaching" the command prompt. I can see the program open (similar to when i only execute the program and not passing any input file name) and then it just closes. Not sure why. Very frustrating but I really appreciate the help.

Regarding the location of the .exe. You are correct in that is a shared network folder. I tried it both ways using the acutal path on my machine and the shard network path. Both seem to be able to execute the program so I left it the shared path for the hope I can get this functional for multiple users. Thanks Again!
 
Upvote 0
Yeah, what I did for my app was make it completely portable. So I didn't have that issue. (And part of that was to have the EXE in the same parent folder, etc.) But sorry I couldn't help. Hopefully someone else will step in and show us how this is done!
 
Upvote 0
Upvote 0
Well if worst comes to worst, you can have VBA programmatically make a copy of the entire folder (containing the .exe and its input files) to the drive of whoever is accessing it, run the EXE from there, and delete that copied folder afterwards . . . This way you can update the EXE as need be, and all users will still be accessing the most up-to-date version.
 
Upvote 0
I dont know that the share folder is the issue. I tested with the .exe on my local drive with the same results. Also, I can open the program if I only call the .exe in the shell from the shared folder and my local, but when i pass the input file name, something seems to go wrong. This is possibly an issue with the program. I am not sure...
 
Upvote 0

Forum statistics

Threads
1,214,670
Messages
6,120,831
Members
448,990
Latest member
rohitsomani

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top