Excel VBA Shell“找不到文件”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28487214/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 06:12:15  来源:igfitidea点击:

Excel VBA Shell "file not found"

vbashellexcel-vbaexcel

提问by B West

For the life of me, I can't get this shell procedure to work with variables. I'm trying to execute POSTRESULTS6 which then processes the csv file. The "DISPLAY" argument tells POSTRESULTS6 to show a progress window as it processes the csv.

在我的一生中,我无法让这个 shell 过程与变量一起工作。我正在尝试执行 POSTRESULTS6 然后处理 csv 文件。“DISPLAY”参数告诉 POSTRESULTS6 在处理 csv 时显示进度窗口。

Dim MyAppID As Variant
Dim stExecute As String: stExecute = """C:\Program Files (x86)\PerkinElmer\LABWORKS64\POSTRESULTS6.exe"""
Dim stFile As String: stFile = "INFILE:C:\PostResult6\Multi-Component_Data_Import.csv"
Dim stFull As String: stFull = "stExecute & "" "" & stFile"
MyAppID = Shell("stFull" & "" "" & DISPLAY, vbNormalFocus)
AppActivate (MyAppID)

The procedure works as expected when I type it all out in one single line like this:

当我像这样在一行中全部输入时,该过程按预期工作:

MyAppID = Shell("""C:\Program Files (x86)\PerkinElmer\LABWORKS64\POSTRESULTS6.exe"" & "" "" & INFILE:C:\PostResult6\Multi-Component_Data_Import.csv" & "" "" & DISPLAY, vbNormalFocus)

The reason that I want to use variables is to shorten the lines of code and possibly make the procedure dynamic for the csv file name.

我想使用变量的原因是为了缩短代码行,并可能使 csv 文件名的过程动态化。

I'm guessing that it has something to do with spaces but I can't figure out where my mistake is. I also can't figure out how to continue the long string of code to the next line. The space-underscore doesn't seem to work.

我猜它与空格有关,但我无法弄清楚我的错误在哪里。我也无法弄清楚如何将长代码字符串延续到下一行。空格下划线似乎不起作用。

As an aside, when my shell procedure was very simple, I didn't have to declare the MyAppID variable. As it got more complex, I started getting errors about MyAppID not being defined. Does anyone know why this is?

顺便说一句,当我的 shell 过程非常简单时,我不必声明 MyAppID 变量。随着它变得越来越复杂,我开始收到有关未定义 MyAppID 的错误。有人知道为什么是这样吗?

Any help is greatly appreciated.

任何帮助是极大的赞赏。

Edit: The Error that I get when I run the code with variables is "Run-time error '53': File not found"

编辑:运行带有变量的代码时出现的错误是“运行时错误'53':找不到文件”

I know that the file paths assigned to the variables are correct. I tested them in windows explorer.

我知道分配给变量的文件路径是正确的。我在 Windows 资源管理器中测试了它们。

采纳答案by Tony Dallimore

Your problem is, for example: stFull = "stExecute & "" "" & stFile"

你的问题是,例如: stFull = "stExecute & "" "" & stFile"

You have stExecutewithin quotes which makes it a literal not a variable.

你有stExecute引号,这使它成为一个文字而不是一个变量。

I think this is correct:

我认为这是正确的:

  Dim MyAppID As Variant
  Dim stExecute As String: stExecute = """C:\Program Files (x86)\PerkinElmer\LABWORKS64\POSTRESULTS6.exe"""
  Dim stFile As String: stFile = "INFILE:C:\PostResult6\Multi-Component_Data_Import.csv"
  Dim stFull As String: stFull = stExecute & " " & stFile
  MyAppID = Shell(stFull & " DISPLAY", vbNormalFocus)

回答by Amen Jlili

You've got messed up quotes. stFullin your last line is being evaluated as a literalnot as a string. Here's my attempt to solve your quotes debacle.

你搞砸了报价。stFull在你的最后一行被评估为文字而不是字符串。这是我试图解决您的报价崩溃的尝试。

    Dim stExecute As String: stExecute = "C:\Program Files (x86)\PerkinElmer\LABWORKS64\POSTRESULTS6.exe"
Dim stFile As String: stFile = "INFILE:C:\PostResult6\Multi-Component_Data_Import.csv"
Dim StFull As String
StFull = """" & """" & """" & stExecute & """" & """" & " & """" """" & " & stFile & """"
MyAppID = Shell(stFull & "" "" & DISPLAY, vbNormalFocus)
AppActivate (MyAppID)

Outputting StFull:

输出 StFull:

enter image description here

在此处输入图片说明

回答by DMM Wallnut

My perspective for using variables in Shell:

我在 Shell 中使用变量的观点:

MyAppID as variant
Dim Program as string
Dim File as string

Program = """Your path to program"""
File = """Your path to file"""

MyAppID = Shell(Program & " " & File, vbNormalFocus)

Additional comments:

补充评论:

  • Single quotes can be used (e.g. Program = "Your path to program") but then VBA may have problem to compile properly if you have spaces in folders names and would return an error.
  • 可以使用单引号(例如 Program = "Your path to program"),但如果文件夹名称中有空格,则 VBA 可能无法正确编译并返回错误。