简体   繁体   English

使用VBA打开exe文件

[英]Open exe file using VBA

I wanted to open a file by VBA and I've written this code:我想用 VBA 打开一个文件,我写了这段代码:

ShellExecute ("explorer.exe C:\FY20 CPM\HS-HO-OPG\B -  SQL INDIRECT BUSINESS\FP 201801\sde.exe")

And for every file it works if I give there notepad or something like this, it will work, for example:对于每个文件,如果我给它记事本或类似的东西,它就会起作用,例如:

ShellExecute ("explorer.exe C:\FY20 CPM\HS-HO-OPG\B -  SQL INDIRECT BUSINESS\FP 201801\Test.txt")

But for that first code, I'm getting this error:但是对于第一个代码,我收到此错误:

Unhandled Exception: System.UnauthorizedAccessException: Access to the path 'C:\\WINDOWS\\System32\\sde-query.log' is denied.未处理的异常:System.UnauthorizedAccessException:拒绝访问路径“C:\\WINDOWS\\System32\\sde-query.log”。

错误

What I want to underline is that I can normally open this file manually.我想强调的是,我通常可以手动打开这个文件。 This file is something like SQL making some queries.该文件类似于 SQL 进行一些查询。

So it is probably a problem with UnauthorizedAccess.所以这可能是 UnauthorizedAccess 的问题。 Is there any stronger function than shell?还有比shell更强大的功能吗? For exmaple open as administator or something?例如,以管理员身份打开什么的? Or maybe someone has a better idea on how to handle it?或者也许有人对如何处理它有更好的想法?

Based on the error message, it appears that the executable that you're trying to run generates its log files in the working directory .根据错误消息,您尝试运行的可执行文件似乎在工作目录中生成了其日志文件。

Now, the code that you posted doesn't make much sense.现在,您发布的代码没有多大意义。 It looks like you're launching Explorer.exe and passing the path of the target file as an argument.看起来您正在启动 Explorer.exe 并将目标文件的路径作为参数传递。 In which case, your code should probably be ShellExecute "explorer.exe", "C:\\FY20..." instead of ShellExecute "explorer.exe C:\\FY20..." .在这种情况下,您的代码可能应该是ShellExecute "explorer.exe", "C:\\FY20..."而不是ShellExecute "explorer.exe C:\\FY20..."

Anyway, the problem with this is that because ShellExecute is actually launching Explorer.exe , not sde.exe , the working directory becomes the system32 directory which the sde.exe process doesn't have permission to access, hence, the UnauthorizedAccessException .无论如何,问题在于,因为ShellExecute实际上是在启动Explorer.exe ,而不是sde.exe ,所以工作目录成为sde.exe进程UnauthorizedAccessException访问的 system32 目录,因此, UnauthorizedAccessException To prevent this, you should launch sde.exe directly without Explorer.exe .为防止出现这种情况,您应该直接启动sde.exe而不使用Explorer.exe Here's a complete example:这是一个完整的例子:

Dim filePath As String
filePath = "C:\FY20 CPM\HS-HO-OPG\B -  SQL INDIRECT BUSINESS\FP 201801\sde.exe"

Dim objShell: Set objShell = CreateObject("shell.application")
objShell.ShellExecute filePath, "", "", "open", 1
Set objShell = Nothing

And you can even change the working directory by passing a path to the third parameter:您甚至可以通过将路径传递给第三个参数来更改工作目录:

Dim customWorkingDir As String
customWorkingDir = "C:\FY20 CPM\HS-HO-OPG\B -  SQL INDIRECT BUSINESS\FP 201801"
objShell.ShellExecute filePath, "", customWorkingDir , "open", 1

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM