(Shell功能)VBA中需要文件路径吗? [英] Is file path needed in (Shell Function) VBA?

查看:345
本文介绍了(Shell功能)VBA中需要文件路径吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Matlab生成的可执行文件, Myfile.exe excel-vba 。我学习(Shell功能)是我需要使用的。



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



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

错误恢复下一步
Shell(C:\Users\elwany\Desktop\Myfolder\Myfile.exe)
如果Err<> 0然后
MsgBox无法启动应用程序,vbCritical,错误
如果
结束Sub

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

  Sub MyExe()
On Error Resume Next
Shell(Myfile.exe)
如果Err<> 0然后
MsgBox无法启动应用程序,vbCritical,错误
如果
结束Sub

有时它有效,有时它不!



例如,昨天我运行了VBA代码,它工作。今天我打开相同的Excel文件,同一个文件夹,一切都一样,它给出无法启动应用程序错误msg !!



  1. 为什么有时候会有效,有时候不行?

  2. 添加了文件路径绝对是必须的?


解决方案

可以


  1. 按照我之前对您的问题的评论使用 ChDir

  2. 使用 Dir 来验证 myfile.exe 是否需要成为。此方法不需要错误处理来处理丢失的文件。

      Sub TestB()
    Dim strPath As String
    strPath = Dir(c:\temp\myfile.exe)
    如果Len(strPath)> 0然后
    Shell strPath
    Else
    MsgBoxPath is not exist
    End If
    End Sub


    Sub TestA()
    On Error Resume Next
    '使用主机工作簿路径
    'ChDir ThisWorkbook.Path
    '设置路径
    ChDirC:\temp
    Shell(Myfile.exe)
    如果Err<> 0然后
    MsgBox无法启动应用程序,vbCritical,错误
    Else
    MsgBoxsucess!,vbOKOnly
    End If
    End Sub



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

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 is I put the executable + the Excel file with the VBA project in the same folder (Myfolder), and then I modify the code to:

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!!

  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?

解决方案

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

  1. Use ChDir as per my earlier comment to your question
  2. Use Dir instead to validate that myfile.exe is 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
    

这篇关于(Shell功能)VBA中需要文件路径吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆