VBA:如何从MS Access运行另一个应用程序 [英] VBA: How to run another application from MS Access

查看:91
本文介绍了VBA:如何从MS Access运行另一个应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力弄清这个问题,看来我在任何地方都找不到解决问题的方法.这是第一部分:​​ VBA Shell命令始终返回找不到文件" ; 在该问题中,出于某种奇怪的原因(可能是安全设置),它没有在%APPDATA%文件夹中找到该应用程序.

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.

此后,我将导入工具移到了存储数据库的同一目录中,希望它能够正常工作.

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.

我的目标是单击MS Access中的一个按钮,并使其直接运行我的导入工具.现在,我打开保存该工具的文件夹.这可以在具有管理员权限的开发机器上使用,但在没有管理员特权的其他机器上不能使用.现在的代码如下所示:

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:

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.

我也尝试了以下方法:

Dim hProcess As Long
Dim myPath as String

myPath = Environ("ProgramFiles(x86)") & "\mytool\mytool.exe"
hProcess = Shell(myPath, vbNormalFocus)

本节似乎"可以正常工作,因为当我查看流程管理器时,它会加载应用程序"mytool.exe".但是,几秒钟后(可能是20秒),将弹出一个对话框,指出应用程序"mytool.exe"已停止工作.

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.

这里要注意的一件事是,我在开发计算机上具有管理员特权,但在我的家用计算机上却拥有所有特权.在我的家用计算机上,第二个代码可以正常工作.在我的开发计算机上,它崩溃了,而在受限用户计算机上,它什么也没做.

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.

对于使用小于管理员权限的同时如何从MS Access打开此应用程序,是否有任何建议?直接运行该应用程序,或者至少打开该应用程序所在的文件夹.

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.

谢谢!

P.S.我已经尝试了在stackoverflow上找到的ShellAndWait和RunApplication代码,但在此实例中都不起作用.

P.S. I have tried both ShellAndWait and RunApplication code found on stackoverflow, neither of which works in this instance.

推荐答案

当我需要在VBA中执行某些操作时,我总是使用Windows API中的ShellExecute. 据我所知,它也可以在没有完全特权的机器上运行.

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.

示例:

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

现在您可以调用ShellEx来运行几乎所有内容:

Now you can call ShellEx to run pretty much anything:

'run executable
ShellEx "c:\mytool.exe"

'open file with default app
ShellEx "c:\someimage.jpg"

'open explorer window
ShellEx "c:\"

请注意,ShellEx也具有两个可选参数.
在上面的示例中我没有显示,但是您可以:

Note that ShellEx has two optional parameters as well.
I didn't show this in the above examples, but you can:

  • 将参数传递给可执行文件
  • 隐藏被调用可执行文件的窗口

这篇关于VBA:如何从MS Access运行另一个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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