(Shell Function) VBA 中是否需要文件路径?

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

Is file path needed in (Shell Function) VBA?

excelshellexcel-vbavba

提问by Alaa Elwany

I have a Matlab-generated executable file, Myfile.exeto call from excel-vba. I learned (Shell Function) is what I need to use.

我有一个 Matlab 生成的可执行文件Myfile.exe可以从excel-vba调用。我学到的 (Shell Function) 是我需要使用的。

I don't want to include the whole file path as I do not want to restrict the user to a certain folder in a certain location on each computer.

我不想包含整个文件路径,因为我不想将用户限制在每台计算机上某个位置的某个文件夹中。

I have the following code to call the executable, which works fine:

我有以下代码来调用可执行文件,它工作正常:

Sub MyExe()
    On Error Resume Next
    Shell ("C:\Users\elwany\Desktop\Myfolder\Myfile.exe")
    If Err <> 0 Then
        MsgBox "Can't start the application.", vbCritical, "Error"
    End If
End Sub

My problem/question isI put the executable + the Excel file with the VBA project in the same folder (Myfolder), and then I modify the code to:

我的问题/问题是我将可执行文件 + Excel 文件与 VBA 项目放在同一文件夹(Myfolder)中,然后将代码修改为:

Sub MyExe()
    On Error Resume Next
    Shell ("Myfile.exe")
    If Err <> 0 Then
        MsgBox "Can't start the application.", vbCritical, "Error"
    End If
End Sub

Sometimes it works, sometimes it doesn't!

有时有效,有时无效!

For example, yesterday I ran the VBA code, it worked. Today I opened the same Excel file, same folder, same everything, it gives "Can't Start Application" error msg!!

例如,昨天我运行了 VBA 代码,它起作用了。今天我打开了相同的 Excel 文件、相同的文件夹、相同的所有内容,它给出了“无法启动应用程序”错误消息!!

  1. Is it not okay to remove the file path even if I have everything in one folder?
  2. Why does it sometimes work, sometimes not?
  3. Is adding the file path absolutely mandatory?
  1. 即使我将所有内容都放在一个文件夹中,也不能删除文件路径吗?
  2. 为什么它有时有效,有时无效?
  3. 添加文件路径是绝对必须的吗?

采纳答案by brettdj

As you have enquire further about different directories note that you can either

当您进一步询问不同的目录时,请注意您可以

  1. Use ChDiras per my earlier comment to your question
  2. Use Dirinstead to validate that myfile.exeis where it needs to be. This method doesn't need error handling to handle the file being missing.

    Sub TestB()
    Dim strPath As String
    strPath = Dir("c:\temp\myfile.exe")
    If Len(strPath) > 0 Then
        Shell strPath
    Else
        MsgBox "Path doesn't exist"
    End If
    End Sub
    
    
    Sub TestA()
    On Error Resume Next
    'use the host workbook path
    ' ChDir ThisWorkbook.Path
    'set path here
    ChDir "C:\temp"
    Shell ("Myfile.exe")
    If Err <> 0 Then
        MsgBox "Can't start the application.", vbCritical, "Error"
    Else
        MsgBox "sucess!", vbOKOnly
    End If
    End Sub
    
  1. ChDir按照我之前对您的问题的评论使用
  2. 使用Dir代替来验证myfile.exe它需要的位置。此方法不需要错误处理来处理丢失的文件。

    Sub TestB()
    Dim strPath As String
    strPath = Dir("c:\temp\myfile.exe")
    If Len(strPath) > 0 Then
        Shell strPath
    Else
        MsgBox "Path doesn't exist"
    End If
    End Sub
    
    
    Sub TestA()
    On Error Resume Next
    'use the host workbook path
    ' ChDir ThisWorkbook.Path
    'set path here
    ChDir "C:\temp"
    Shell ("Myfile.exe")
    If Err <> 0 Then
        MsgBox "Can't start the application.", vbCritical, "Error"
    Else
        MsgBox "sucess!", vbOKOnly
    End If
    End Sub
    

回答by chris neilsen

When you run a shell like this without a path specified it runs from the Active Directory. What the Active Directory is depends on the OS, not Excel/VBA (unless you explicitly set it)

当您在没有指定路径的情况下运行这样的 shell 时,它会从 Active Directory 运行。Active Directory 是什么取决于操作系统,而不是 Excel/VBA(除非您明确设置它)

Try this instead

试试这个

Sub MyExe()
    On Error Resume Next
    Shell (ThisWorkbook.Path & "\Myfile.exe")
    If Err <> 0 Then
        MsgBox "Can't start the application.", vbCritical, "Error"
    End If
End Sub