简体   繁体   中英

Compile error: expected list separator or ) excel VBA

I have got this command line in VBA:

Call Shell("cmd.exe /S /k " & ""C:\Program Files\Java\jre1.8.0_171\bin\javaw.exe" -jar " & DPath & "  """ & inp1 & """ """ & inp2 & """ """ & sPath & """ """ & FilePath & """", vbNormalFocus)

When I remove the quotations the error appears in VBA:

 'C:\Program' is not recognized as an internal or external command, operable program or batch file.

When I add the quotations the error appears in VBA:

 Compile error: expected list separator or )

How can I solve the error?

The quotation is wrong, you must put another pair of quotes around the javaw.exe path, because in VBA, a literal " must be specified as "" :

Call Shell("cmd.exe /S /K " & """C:\Program Files\Java\jre1.8.0_171\bin\javaw.exe"" -jar " & DPath & "  """ & inp1 & """ """ & inp2 & """ """ & sPath & """ """ & FilePath & """", vbNormalFocus)

But this is still not going to work, because cmd removes the outer-most quotation marks and leaves behind an invalid command line. Therefore, you must provide another pair of (literal) quotation marks around the whole expression behind /K , which may be removed by cmd :

Call Shell("cmd.exe /S /K " & """""C:\Program Files\Java\jre1.8.0_171\bin\javaw.exe"" -jar " & DPath & "  """ & inp1 & """ """ & inp2 & """ """ & sPath & """ """ & FilePath & """""", vbNormalFocus)

Finally, let me recommend to enclose the DPath value within (literal) quotation marks as well:

Call Shell("cmd.exe /S /K " & """""C:\Program Files\Java\jre1.8.0_171\bin\javaw.exe"" -jar """ & DPath & """ """ & inp1 & """ """ & inp2 & """ """ & sPath & """ """ & FilePath & """""", vbNormalFocus)

Please learn how to make your life easy. Just because you can represent a " in a VBA string using """ doesn't mean you should except in the most trivial instance.

Here is a native VBA method for making life simpler.

Dim qDPath As String
Dim qinp1 As String
Dim qinp2 As String
Dim qsPath As String
Dim qFilePath As String
Dim qCommand As String

qDPath = MakeQuotedString(Path)
qinp1 = MakeQuotedString(inp1)
qinp2 = MakeQuotedString(inp2)
qsPath = MakeQuotedString(sPath)
qFilePath = MakeQuotedString(FilePath)

qCommand = MakeQuotedString("C:\Program Files\Java\jre1.8.0_171\bin\javaw.exe")
qCommand = MakeQuotedString("cmd.exe /S /k " & qCommand & "-Jar " & qDPath & qinp1 & qinp2 & qsPath & qFilePath)

Call Shell(qCommand, vbNormalFocus)

Whilst the above is much more verbose it at least allows you to use single stepping via F8 to check at each stage that you are constructing the final command string correctly.

As you can see the above is a bit verbose which is why many other programming languages have the ability to embed variable and formatting markers in a string. This can also be done in VBA using a Module to hide the relevant code. Please have a look at the Fmt method in my Layout Module for VBA. Its available in GitHub from here . Its a very simple library module to allow the embedding of variable fields and formatting markers in strings

This will allow you to write

Call Shell("cmd.exe /S /k " & ""C:\Program Files\Java\jre1.8.0_171\bin\javaw.exe" -jar " & DPath & "  """ & inp1 & """ """ & inp2 & """ """ & sPath & """ """ & FilePath & """", vbNormalFocus)

In a much more concise way, which is also friendly to the human eye.

Dim ShellCmd as String
ShellCmd = Fmt("cmd.exe /S /k {dq}{dq}C:\Program Files\Java\jre1.8.0_171\bin\javaw.exe{dq} -jar {dq}{0}{dq}  {dq}{1}{dq} {dq}{2}{dq} {dq}{3}{dq} {dq}{4}{dq}{dq}", dpath, inp1, inp2, sPath, FilePath)

Call Shell(ShellCmd, vbNormalFocus)

Apologies in advance if I've still managed to get the {dq} mismatched.

Call Shell("cmd.exe /S /k ""C:\Program Files\Java\jre1.8.0_171\bin\javaw.exe"" -jar " & _
           DPath & "  """ & inp1 & """ """ & inp2 & """ """ & sPath & """ """ & _
            FilePath & """ ", vbNormalFocus)

...depending on the various values of your concatenated variables

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM