Ms Excel VBA - 使用 Shell 命令打开文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40169335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ms Excel VBA - Using Shell command to open files
提问by Xain Cheema
I am developing a workbook in Excel-VBA and i want to execute a script using HEC DSS. That means, using excel-vba, i need to open "HEC DSS" first and then i'll instruct the application to open the script.
我正在 Excel-VBA 中开发工作簿,我想使用 HEC DSS 执行脚本。这意味着,使用 excel-vba,我需要先打开“HEC DSS”,然后我将指示应用程序打开脚本。
We keep it simple and try to correlate the above scnerio with a NotePAd.exe and a txt file. For the purpose, i have googled and tried different things but none worked. I am trying to use the SHELL command. Please find the code below:
我们保持简单并尝试将上述 scnerio 与 NotePAd.exe 和 txt 文件相关联。为此,我用谷歌搜索并尝试了不同的东西,但都没有奏效。我正在尝试使用 SHELL 命令。请在下面找到代码:
Sub test()
Dim retval as string
dim file name as variant
filename="C:\Users\Nayar Asif\Desktop\Test_2.txt"
retval = Shell("notepad.exe" & filename, vbnormalfocus)
end sub
The above code does not work. The idea is to open the notepad application and then open the notepad file. Any help????
上面的代码不起作用。这个想法是打开记事本应用程序,然后打开记事本文件。有帮助吗???
Regards Nayyar
问候内亚尔
采纳答案by Tim Williams
File paths with spaces should be in quotes
带空格的文件路径应该用引号引起来
retval = Shell("notepad.exe """ & filename & """", vbnormalfocus)
回答by AymericB
The solution is just the "space" after notepad.exe and before the second quote :
解决方案只是 notepad.exe 之后和第二个引号之前的“空格”:
retval = Shell("notepad.exe " & filename, vbnormalfocus)