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

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

问题描述

我以一个小的C#应用​​程序链接到我的数据库作为辅助工具使用VBA的MS Access。我已经尝试了几个来自计算器自己不同的想法,包括ShellAndWait工具,另一个页面上。

我有一个窗体上的按钮。当你点击这个按钮,它应该运行,我目前存储在另一个应用程序%APPDATA%/程序/

这是在code当前活动:

 私人小组BtnImport_Click()

昏暗的文件作为字符串
昏暗hProcess为Long

文件= ENVIRON(APPDATA)及\程序\ component_import.exe
这是标准的版本,这显然无助于这个时候。
hProcess =壳牌(文件,vbNormalFocus)

这是我从这里早些时候拿到了RunApplication版本。它结束
与成功返回-532462766
import_funcs.RunApplication(文件)

这是ShellAndWait版本,这给了我一个找不到文件的错误
import_funcs.ShellAndWait(文件,0,vbNormalFocus,AbandonWait)

结束小组
 

我已经改变了原有的掏出了两个ShellAndWait模块与其他类似模块。在我的应用程序而言不是开始也不对这些选项的工作有什么不同。

我已经反复检查文件是正确的(它指向 C:\用户\我\应用程序数据\漫游\程序\ component_import.exe )。我有双重检查,以确保我的应用程序是在正确的位置。

运行良好,如果我从文件管理器中双击。它说运行时错误53:文件未找到每当我试图从MS Access运行

有什么建议?

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

编辑:增加了一些额外的code。 链接到第一引擎收录: RunApplication引擎收录 链接到第二引擎收录: ShellAndWait引擎收录

解决方案

我发现有时文件夹名称用空格使用shell命令时,会抛出错误。

如:C:\我的文件夹\ appl.exe

使其: C:\ MyFolder的\ appl.exe

还可以检查一个有效的路径: 下面code盘,其中的chrome.exe居住,并要求www.google.com从那里通过传递URL作为参数文件夹:

 公用Sub Display_Google()
  昏暗chromePath作为字符串
  chromePath =C:\ Program Files文件\谷歌\浏览器\应用程序\的chrome.exe

  如果FILEEXISTS(chromePath)然后
  壳(chromePath&安培;-url与&与&www.google.com),vbMaximizedFocus
  其他

  chromePath =C:\ Program Files文件(x86)的\谷歌\浏览器\应用程序\的chrome.exe
  壳(chromePath&安培;-url与&与&www.google.com),vbMaximizedFocus
  结束如果
结束小组
 

  

 公共功能FILEEXISTS(BYVAL文件名作为字符串)作为布尔
    在错误恢复下一页
    FILEEXISTS =不CBool​​函数(GETATTR(文件名)和(vbDirectory或者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天全站免登陆