VBA Shell 命令总是返回“找不到文件" [英] VBA Shell command always returns "File Not Found"

查看:55
本文介绍了VBA Shell 命令总是返回“找不到文件"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 VBA 用于 MS Access,以便将一个小型 C# 应用程序作为辅助工具链接到我的数据库.我从 stackoverflow 本身尝试了几个不同的想法,包括 ShellAndWait 实用程序和该页面上的另一个.

我在表单上有一个按钮.当您单击此按钮时,它应该运行我当前存储在 %APPDATA%/program/

中的另一个应用程序

这是当前有效的代码:

Private Sub BtnImport_Click()Dim 文件作为字符串Dim hProcess 一样长file = Environ("APPDATA") &\program\component_import.exe"'这是标准版本,目前显然什么都不做.hProcess = Shell(file, vbNormalFocus)'这是我之前从这里得到的 RunApplication 版本.它结束'with "成功返回-532462766import_funcs.RunApplication(文件)'这是 ShellAndWait 版本,它给了我一个找不到文件"错误import_funcs.ShellAndWait(file, 0, vbNormalFocus, AbandonWait)结束子

我已经为 ShellAndWait 模块和另一个类似的模块更改了原始外壳.就我的应用程序未启动而言,这两个选项的工作方式都没有任何不同.

我已仔细检查文件"是否正确(它指向 C:\Users\Me\AppData\Roaming\program\component_import.exe).我已经仔细检查以确保我的应用程序位于正确的位置.

如果我从文件资源管理器双击它运行良好.每当我尝试从 MS Access 运行它时,它都会显示 Run-time error '53': File not found..

有什么建议吗?

顺便说一句,路径本身不包含任何空格.

添加了一些额外的代码.链接到第一个 pastebin:RunApplication pastebin链接到第二个 pastebin:ShellAndWait pastebin

解决方案

我发现有时使用 shell 命令时,带有空格的文件夹名称会引发错误.

例如:C:\My Folder\appl.exe

制作:C:\MyFolder\appl.exe

还可以检查有效路径:以下代码通过将 url 作为参数传递来检查 chrome.exe 所在的文件夹并从那里调用 www.google.com:

Public Sub Display_Google()将 chromePath 调暗为字符串chromePath = "C:\Program Files\Google\Chrome\Application\chrome.exe"如果 FileExists(chromePath) 那么Shell (chromePath & " -url" & " " & "www.google.com"),vbMaximizedFocus别的chromePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"Shell (chromePath & " -url" & " " & "www.google.com"),vbMaximizedFocus万一结束子

<块引用>

公共函数 FileExists(ByVal FileName As String) As Boolean出错时继续下一步FileExists = Not CBool​​(GetAttr(FileName) And (vbDirectory or vbVolume))出错时转到 0结束函数

I'm using VBA for MS Access in order to link a small C# app to my database as a helper tool. I have tried a couple of different ideas from stackoverflow itself, including the ShellAndWait utility and another on that page.

I have a button on a form. When you click this button, it should run another application that I am currently storing in %APPDATA%/program/

This is the code that is currently active:

Private Sub BtnImport_Click()

Dim file As String
Dim hProcess as Long

file = Environ("APPDATA") & "\program\component_import.exe"
'This is the standard version, which apparently does nothing at this time.
hProcess = Shell(file, vbNormalFocus)

'This is the RunApplication version I got from here earlier. It ends
'with "Successfully returned -532462766
import_funcs.RunApplication(file)

'This is the ShellAndWait version, which gives me a "File not Found" error
import_funcs.ShellAndWait(file, 0, vbNormalFocus, AbandonWait)

End Sub

I had changed the original shell out for both the ShellAndWait module and another similar module. Neither of those options work any differently in terms of my application not starting.

I have double-checked that "file" is correct (It points to C:\Users\Me\AppData\Roaming\program\component_import.exe). I have double-checked to make sure that my app is in the correct location.

It runs fine if I double-click from file explorer. It says Run-time error '53': File not found. whenever I attempt to run it from MS Access.

Any suggestions?

Edit: As an aside, the path itself does not contain any spaces.

Edit: Added some additional code. Link to first pastebin: RunApplication pastebin Link to second pastebin: ShellAndWait pastebin

解决方案

I found sometimes folder names with spaces throws error when using shell command.

eg: C:\My Folder\appl.exe

make it: C:\MyFolder\appl.exe

Also can check for a valid path: The following code checks the folder where chrome.exe residing and calling www.google.com from there by passing url as argument:

Public Sub Display_Google()
  Dim chromePath As String
  chromePath = "C:\Program Files\Google\Chrome\Application\chrome.exe"

  If FileExists(chromePath) Then
  Shell (chromePath & " -url" & " " & "www.google.com"), vbMaximizedFocus
  Else

  chromePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
  Shell (chromePath & " -url" & " " & "www.google.com"), vbMaximizedFocus
  End If
End Sub

Public Function FileExists(ByVal FileName As String) As Boolean
    On Error Resume Next
    FileExists = Not CBool(GetAttr(FileName) And (vbDirectory Or vbVolume))
    On Error GoTo 0
End Function

这篇关于VBA Shell 命令总是返回“找不到文件"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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