1

I can send a string argument ARGUMENT in an Excel VBA Shell command to start a c# application:

Call Shell("""" & filePathExe & """ """ & ARGUMENT & """", vbNormalFocus)

but is it possible to send an array? It looks like c# applications can receive an array of arguments:

[STAThread]
static void Main(string[] args) { }

So my question is - is it possible to send an array via the Shell command or is there a better way to be starting my application via Excel?

4
  • Wait a minute, I've just noticed all the " instances... can I separate all my arguments with " - YES I can is the answer to that. Now all I need to know is if this stupid question has been asked before. Sure it has.
    – jamheadart
    Oct 25, 2018 at 13:54
  • I think you would have to make your own function that accepts a paramArray of string arguments and then loops through each element of the paramArray and executes the Shell command on them.
    – ArcherBird
    Oct 25, 2018 at 13:57
  • What exactly are you trying to do? This smells of something that would be better off as [ComVisible].
    – Comintern
    Oct 25, 2018 at 14:14
  • @Comintern it really would be much better off as that, I know it would be, but I'm inexperienced in distributing my custom class libraries to other users and getting Excel to be able to use them without a hitch. I can set it all up on my own Computer but seems like a lot of work to be able to distribute this to others successfully. Regasm and all that jazz :/
    – jamheadart
    Oct 25, 2018 at 14:17

1 Answer 1

1

You could make your own function that takes a paramArray of shell commands and loops through, executing the shell command on each. Like this:

Sub ExecuteShell(ParamArray arg() As Variant)
    Dim element As Variant

    For Each element In arg
        Call Shell(element)
    Next element
End Sub

Of course, you'll want to maybe trap for "bad" shell arguments.

You would run the above like this:

Sub Test()
    Dim arg1, arg2, arg3 As String

    arg1 = "filepath1..."
    arg2 = "filepath2..."
    arg3 = "filepath3..."

    Call ExecuteShell(arg1, arg2, arg3)
End Sub

Alternatively, you could structure the function to accept a filepath argument and an array of arguments then all get concatenated together into a single command, like this:

Sub ExecuteShell(path As String, ParamArray arg() As Variant)
    Dim element As Variant
    Dim cmd As String

    cmd = path & " " 'insert any pre-arguments charaters

    For Each element In arg
        cmd = cmd & element & ", "
    Next element

    'remove last comma
    cmd = Left(cmd, Len(cmd) - 2)

    Call Shell(cmd)

End Sub
4
  • 1
    This is basically what I'm doing, although rather than calling shell on each element I was just adding each argument in the array to my string? So e.g. my array contains 1,2,3 and the final command would be Call Shell ("c:\whatever" "1" "2" "3", vbNormalFocus)
    – jamheadart
    Oct 25, 2018 at 14:05
  • Yeah I think your answer is tailored towards running the same application more than once - or will this work on the same instance of the app and just send it multiple arguments one by one.. I need to investigate this.
    – jamheadart
    Oct 25, 2018 at 14:06
  • Yes, this would run the same application more than once. If you wanted to send multiple arguments in the same command line, I guess you would want to do something similar to the above, but instead "unpack" the array and concat its contents into a single string that you then send to the Shell command.
    – ArcherBird
    Oct 25, 2018 at 14:09
  • @jamheadart based on your comments, I added another suggested version of a custom function to do this...
    – ArcherBird
    Oct 25, 2018 at 14:16

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.