使用 WScript.Shell.Run 的 Excel VBA 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19054973/
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
Excel VBA error using WScript.Shell.Run
提问by Terry
After recently upgrading from Excel 2010 to Excel 2013, I moved a custom add-in (.xlam) to the new Application.LibraryPath directory (C:\Program Files\Microsoft Office 15\root\office15\Library\BTRTools). There is a bit of code that launches an executable (exe) file (located in sub directory of the add-in). However, since the upgrade/move, I am not getting an error:
最近从 Excel 2010 升级到 Excel 2013 后,我将自定义加载项 (.xlam) 移动到新的 Application.LibraryPath 目录 (C:\Program Files\Microsoft Office 15\root\office15\Library\BTRTools)。有一些代码可以启动可执行 (exe) 文件(位于加载项的子目录中)。但是,自升级/移动以来,我没有收到错误消息:
PrettyPrintXml.exe - Application Error
PrettyPrintXml.exe - Application Error
The application was unable to start correctly (0xc000007b). Click OK to close the application.
The application was unable to start correctly (0xc000007b). Click OK to close the application.
I'm obviously pretty convinced it is file permissions. I have explicitly added myself permissions with full rights to the \Library folder (and all subs). Note that I think I had to do this even with Excel 2010 (folder at C:\Program Files (x86)\Microsoft Office\Office14\Library) to make things work.
我显然非常确信这是文件权限。我已经明确添加了自己对 \Library 文件夹(和所有子文件)具有完全权限的权限。请注意,我认为即使使用 Excel 2010(位于 C:\Program Files (x86)\Microsoft Office\Office14\Library 的文件夹),我也必须这样做才能使工作正常进行。
However, after all this, I'm still stuck and can not launch the exe file. Any ideas/suggestions on how to make this work?
但是,毕竟这一切,我仍然卡住,无法启动 exe 文件。关于如何使这项工作有任何想法/建议?
Code is pretty standard:
代码非常标准:
Public Sub RunShellExecute(sFile As String, Optional params As String = "", Optional wait As Boolean = False)
Dim wsh As Object: Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = wait
Dim windowStyle As Integer: windowStyle = 1
Dim exe As String: exe = IIf(Left(sFile, 1) <> """", """" & sFile & """", sFile)
Dim exeParams As String: exeParams = IIf(params <> "", " " & params, "")
Dim errorCode As Integer: errorCode = wsh.Run(exe & exeParams, windowStyle, waitOnReturn)
If errorCode = 0 Then
'// MsgBox "Done! No error to report."
Else
MsgBox "Program exited with error code " & errorCode & "."
End If
End Sub
回答by Blackhawk
I know your question is "Why doesn't this work", but I thought you might be interested in an alternate solution: There is a native VBA PrettyPrintXML. You need to add a reference to the MSXML library in your VBA project by clicking "Tools" ---> "References..." and then check the box next to Microsoft XML, v6.0
(or whatever version is included with your version of Office/Windows).
我知道您的问题是“为什么这不起作用”,但我认为您可能对替代解决方案感兴趣: 有一个本机 VBA PrettyPrintXML。您需要通过单击“工具”--->“引用...”在您的 VBA 项目中添加对 MSXML 库的引用,然后选中旁边的框Microsoft XML, v6.0
(或您的 Office/Windows 版本中包含的任何版本) .
回答by Anonymous
Please change the title of your question, because Excel VBA isable to use WScript.Shell.Run, otherwise you wouldn't be getting your error.
请改变你的问题的标题,因为Excel VBA是能够使用WScript.Shell.Run,否则你不会让你的错误。
As for the actual issue, this looks like a 32-bit / 64-bit problem. Investigate whether the program you're calling is appropriate for your system and whether it tries to load the right DLLs.
至于实际问题,这看起来像是一个 32 位 / 64 位问题。调查您正在调用的程序是否适合您的系统以及它是否尝试加载正确的 DLL。
The problem is not file permissions, then you would get a different status code.
问题不在于文件权限,那么您将获得不同的状态代码。
回答by Marijan
You should use a path without spaces in it, something simple like 'C:\BTRTools'. Then it should work.
你应该使用一个没有空格的路径,比如“C:\BTRTools”。那么它应该工作。