none
passing arguments to an executable file RRS feed

  • Question

  • Hi,

           I need to pass arguments to an .exe file from excel vba...... How to accomplish this using the shell command???

    Monday, September 26, 2011 6:43 AM

Answers

  • Assuming your macro is VBA in Excel

    Sub test()
    Dim nVer As Long
    Dim s As String
            nVer = Val(Application.Version)
            Select Case nVer
         Case 8: s = "97"
         Case 9: s = "2000"
         Case 10: s = "2002"
         Case 11: s = "2003"
         Case 12: s = "2007"
         Case 14: s = "2010"
         Case Is > 14: s = "newer than 2010" ' cater for future
         End Select
            MsgBox nVer & vbCr & s
    
    End Sub

    Depending on what you are doing, in 2007/2010 you might also want to check if a particular workbook is an xls/a or xlsx/m. There are various ways, simplest perhaps is check how many rows the first worksheet has.

    Peter Thornton

    Wednesday, September 28, 2011 8:32 AM
    Moderator

All replies

  • From VBA help on the Shell command:

    Name of the program to execute and any required arguments or command-line switches; may include directory or folder and drive.


    Rod Gill

    The one and only Project VBA Book Rod Gill Project Management
    Monday, September 26, 2011 9:25 AM
  • Hi Rod Gill,

                     Thanks for the reply.. But i've tried it b4 and it isn't workin... My calling is somethin like this...

    test1=shell("C:\Program Files\TPMT\mui\excelver.exe 107",1)

    107 is the parameter i need to pass to that exe file

    Monday, September 26, 2011 9:31 AM
  • Try with the argument in double quotes

    "C:\Program Files\TPMT\mui\excelver.exe ""107"""

    Peter Thornton

    Monday, September 26, 2011 10:56 AM
    Moderator
  • Hi Peter,

                  I tried it... It isn't workin....... actually it is a vb script that was compiled to exe.. Am passing that argument to prevent users running that exe by double clicking on it...

    Monday, September 26, 2011 11:50 AM
  • What do you type in Start/Run line to make it work

    Peter Thornton

    Monday, September 26, 2011 12:41 PM
    Moderator
  • i can't get u peter... can u explain??
    Monday, September 26, 2011 2:13 PM
  • Press the windows start button, then Run (usually lower right on the start menu)

    In Windows 7 the Run command is not included by default but you can do Start-button + R (or customize the Start Menu to include the Run command).

    Peter Thornton

    Monday, September 26, 2011 2:58 PM
    Moderator
  • Do you mean, how am running it from the command line???? If so, I can't run it from the command line too....

    I tried running the .vbs file before compilin by passing arguments and it is workin fine.. but after compilin it to .exe I can't run it

    Tuesday, September 27, 2011 5:39 AM
  • The Run line in effect calls Shell directly from Windows, the command line is from the Command window (search "cmd" or type "cmd" in the Run line). They work similarly though there are some differences.

    Anyway, if you can't call your app and arguments from the Run or Command line explains why Shell doesn't work.

    How did you compile your .vbs to an .exe. In the .exe how do you retrieve the arguments, what's the code.

    Peter Thornton

    Tuesday, September 27, 2011 7:25 AM
    Moderator
  • I compiled it using a vbs to exe converter downloaded from internet..... The vb script actually cheks for the installed office version and invokes the corresponding exe to display result....

    So, at first the vbs checks for 107 in command line argument.. if it is found then it executes the other statements else it'll end....

    This is what my vb script does..

    Tuesday, September 27, 2011 7:46 AM
  • I guess the exe needs to include a routine to accept the arguments, then pass as the script is temporarily unpacked and run. It sounds like your converter does not cater for an argument string.

    As a workaround, rather than passing arguments maybe include something like this at the top of your vbs

    arg = inputbox("enter argument(s)", "my script")
    msgbox arg

    Peter Thornton

    Tuesday, September 27, 2011 9:13 AM
    Moderator
  • Hi peter,

             Thats a good idea... But how to prevent the input box from appearing.. I don't want the users to know that this script is running......

    Tuesday, September 27, 2011 2:13 PM
  • I didn't know you don't want the user to know the script is running. I take it you intend to call or run the script from your own app, so why do you need a vbs, I mean why can't our app to the same work as the vbs. In other words why do you particularly need a vbs/exe

    Peter Thornton

    Tuesday, September 27, 2011 2:43 PM
    Moderator
  • Hi,

        Actually I have two exe's that'll display the result of the macro... Those were built from VB 2008..... One is for excel 2003 and the other is for Excel 2007..... So I need to check for the office version installed in the user's system and invoke the corresponding exe to display the result.....

    So am doing the checking of installed office version using vb script.. And I don't want the users to edit that script, so am converting it to exe..

    Wednesday, September 28, 2011 5:11 AM
  • I don't follow  You say "display the result", what result (surely not simply the application version) , how is the vbs/exe called - by the macro?

    The macro can easily be coded to cater for the version. I still don't understand why you need the vbs/exe when presumably the same results can be returned by the macro.

    Peter Thornton

    Wednesday, September 28, 2011 6:56 AM
    Moderator
  • It is an analysis macro.. It'll analyse some performances of MW links and'll produce a summary with graph... I'll display that summary and graph as result.......

    I don't know how to check for office version in macro.. Thats why i used vbs... Please let me know thwe way to check for offiv=ce version from macro

    Wednesday, September 28, 2011 7:12 AM
  • Assuming your macro is VBA in Excel

    Sub test()
    Dim nVer As Long
    Dim s As String
            nVer = Val(Application.Version)
            Select Case nVer
         Case 8: s = "97"
         Case 9: s = "2000"
         Case 10: s = "2002"
         Case 11: s = "2003"
         Case 12: s = "2007"
         Case 14: s = "2010"
         Case Is > 14: s = "newer than 2010" ' cater for future
         End Select
            MsgBox nVer & vbCr & s
    
    End Sub

    Depending on what you are doing, in 2007/2010 you might also want to check if a particular workbook is an xls/a or xlsx/m. There are various ways, simplest perhaps is check how many rows the first worksheet has.

    Peter Thornton

    Wednesday, September 28, 2011 8:32 AM
    Moderator
  • Thanks Peter,

                        Will use it in the macro...

    Wednesday, September 28, 2011 8:40 AM