简体   繁体   中英

How to create a triple nested command line string for Shell function in VBA?

I need to create a VBA Macro in Excel.

The task is that when a button is clicked a command will be executed in cmd.

The command to be executed is rfrompcb and it takes the path of a file as argument so there is the first level string which wraps the path up as it contains spaces. As this command is to be executed in cmd there is the second level string, which is the argument for cmd command, ie cmd /c "rfrompcb ""file_path""" (I hope I have got this one right). Then since it is to be called by Shell in VBA there is the third level string which wraps the cmd command and serves as the argument for Shell.

My question is : How many double quotation marks should there be? I am quite confused about what the final command line string would look like. Can anyone show me how to construct such string? Or is there another way to do it which involves less string nesting?

Thanks.

You would only need to quote file_path , for example:

shell environ$("COMSPEC") & " /c dir ""c:\program files\"" /b"

environ$("COMSPEC") just returns the full path for cmd.exe .

If the executable path you wish to run does not need to be quoted then to pass it an argument that does need to be quoted:

Shell Environ$("COMSPEC") & " /c rfrompcb ""file path"""

If the exe path does need to be quoted you need to wrap it all in another pair of quotes so it looks like:

cmd.exe /c ""c:\path to\rfrompcb" "file path""

Which can be done:

Shell Environ$("COMSPEC") & " /c """"c:\path to\rfrompcb"" ""file path""""" 

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