VBA:如何从 MS Access 运行另一个应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10641147/
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
VBA: How to run another application from MS Access
提问by Jim P
I've been trying to get this issue figured out, and it seems that I cannot find the solution to the problem anywhere. Here was the first part: VBA Shell command always returns "File Not Found"In that question, it wasn't finding the application in the %APPDATA% folder for some odd reason, likely a security setting.
我一直在试图弄清楚这个问题,但似乎在任何地方都找不到解决问题的方法。这是第一部分:VBA Shell 命令总是返回“未找到文件”在那个问题中,由于某些奇怪的原因,它没有在 %APPDATA% 文件夹中找到应用程序,可能是安全设置。
I have since moved the import tool to the same directory that I store the database in, with the small hope of getting it to work correctly.
从那以后,我将导入工具移到了我存储数据库的同一目录中,希望能使其正常工作。
My objective is to click a button in MS Access and have it run my import tool directly. Right now, I have it opening the folder that holds the tool. This works on my development machine which has admin privileges, but does not work on other machines without admin privs. The code right now looks something like the following:
我的目标是单击 MS Access 中的一个按钮并让它直接运行我的导入工具。现在,我让它打开保存该工具的文件夹。这适用于我的具有管理员权限的开发机器,但不适用于没有管理员权限的其他机器。现在的代码如下所示:
Dim hProcess as Long
Dim myPath as String
Dim ex as String
ex = "C:\WINDOWS\explorer.exe "
myPath = Environ("ProgramFiles(x86)") & "\mytool\"
hProcess = Shell(ex & myPath, vbNormalFocus)
This allows the folder that holds the file to be opened, but only on machine accounts which have full administrator privileges. When running this on a machine account that has less than full privileges, it simply does nothing.
这允许打开保存文件的文件夹,但仅限于具有完全管理员权限的计算机帐户。在没有完全权限的机器帐户上运行它时,它什么也不做。
I have also tried the following:
我还尝试了以下方法:
Dim hProcess As Long
Dim myPath as String
myPath = Environ("ProgramFiles(x86)") & "\mytool\mytool.exe"
hProcess = Shell(myPath, vbNormalFocus)
This section "seems" to work in that it loads the application "mytool.exe" when I look at the process manager. However, after a few seconds (maybe 20), a dialog pops up stating that the application "mytool.exe" has stopped working.
当我查看进程管理器时,这部分“似乎”起作用,因为它加载了应用程序“mytool.exe”。但是,几秒钟(可能是 20 秒)后,会弹出一个对话框,指出应用程序“mytool.exe”已停止工作。
One thing to note here is that I have admin privileges on my development machine, but I have all privileges on my home machine. On my home machine, this second code works with no issue whatsoever. At my development machine, it crashes, while on a restricted user machine, it doesn't do anything at all.
这里要注意的一件事是,我在我的开发机器上拥有管理员权限,但在我的家用机器上我拥有所有权限。在我的家用机器上,这第二个代码没有任何问题。在我的开发机器上,它崩溃了,而在受限制的用户机器上,它什么也不做。
Are there any suggestions on how to open this application from MS Access while using less-than-admin privileges? Either to run the application directly or to at least open the folder in which said application resides.
是否有关于如何在使用低于管理员权限的情况下从 MS Access 打开此应用程序的建议?直接运行应用程序或至少打开所述应用程序所在的文件夹。
Thanks!
谢谢!
P.S. I have tried both ShellAndWait and RunApplication code found on stackoverflow, neither of which works in this instance.
PS 我已经尝试了在 stackoverflow 上找到的 ShellAndWait 和 RunApplication 代码,但在这种情况下这两种代码都不起作用。
回答by Christian Specht
I always use ShellExecute
from the Windows API when I need to execute something in VBA.
As far as I know, it works on machines without full privileges as well.
ShellExecute
当我需要在 VBA 中执行某些内容时,我总是使用Windows API。
据我所知,它也适用于没有完全权限的机器。
Example:
例子:
Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, _
ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal lpnShowCmd As Long) As Long
Public Sub ShellEx(ByVal Path As String, Optional ByVal Parameters As String, Optional ByVal HideWindow As Boolean)
If Dir(Path) > "" Then
ShellExecute 0, "open", Path, Parameters, "", IIf(HideWindow, 0, 1)
End If
End Sub
Now you can call ShellEx
to run pretty much anything:
现在你可以调用ShellEx
运行几乎任何东西:
'run executable
ShellEx "c:\mytool.exe"
'open file with default app
ShellEx "c:\someimage.jpg"
'open explorer window
ShellEx "c:\"
Note that ShellEx
has two optional parameters as well.
I didn't show this in the above examples, but you can:
请注意,它ShellEx
也有两个可选参数。
我没有在上面的例子中展示这一点,但你可以:
- pass parameters to executables
- hide the window of the called executable
- 将参数传递给可执行文件
- 隐藏被调用的可执行文件的窗口
回答by ajokar
follow this link
按照这个链接
MS ACCESS: LAUNCH AN APPLICATION FROM ACCESS 2003/XP/2000/97
MS ACCESS:从 ACCESS 2003/XP/2000/97 启动应用程序
here is an example of running an application in ms access
这是在 ms access 中运行应用程序的示例
Private Sub Command1_Click()
Dim myPath As String
myPath = "C:\Program Files\mytool\mytool.exe"
Call Shell(myPath , 1)
End Sub
i wish it helps you
我希望它能帮助你
回答by Fionnuala
Just messing about with http://www.mombu.com/microsoft/scripting-wsh/t-vista-uac-problem-with-wscriptshell-run-method-1508617.html. I tried this on Windows 7 home PC.
只是搞乱http://www.mombu.com/microsoft/scripting-wsh/t-vista-uac-problem-with-wscriptshell-run-method-1508617.html。我在 Windows 7 家用 PC 上试过这个。
Set objShell = CreateObject("Shell.Application")
myPath = Environ("ProgramFiles(x86)") & "\mytool"
Set objFolder = objShell.Namespace(myPath)
Set objFolderItem = objFolder.ParseName("mytool.exe")
objFolderItem.InvokeVerb "runas"
It might give you some ideas.
它可能会给你一些想法。
回答by MaxD
Check
查看
Shell szFileName, vbNormalFocus
with some other executable. It can be some problem with your tool.
与其他一些可执行文件。你的工具可能有问题。