vba 在 Excel 中执行嵌入的 .exe 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25995630/
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
In Excel execute embedded .exe object
提问by Nathan Cooper
I have inserted an object into Excel. This object is an exe (a console application).
我已将一个对象插入到 Excel 中。这个对象是一个 exe(一个控制台应用程序)。
I can call the application by double clicking on it. However, I need to call it with parameters (namely the file path of the document it is being called by). How do I call this exe with parameters?
我可以通过双击来调用该应用程序。但是,我需要使用参数调用它(即调用它的文档的文件路径)。我如何用参数调用这个exe?
采纳答案by Miki
If you import .exe to Excel with these steps:
如果使用以下步骤将 .exe 导入 Excel:
- Insert - Object
- Select tab: Create from File
- Browse for exe file
- Check on "Display as icon"
- 插入 - 对象
- 选择选项卡:从文件创建
- 浏览exe文件
- 勾选“显示为图标”
then you can write VBA subroutine/macro like (I used rectangle shaped object to execute macro by clicking it):
然后你可以编写 VBA 子程序/宏(我使用矩形对象通过单击它来执行宏):
Sub RoundedRectangle1_Click()
Dim ws As Worksheet
Dim oo As OLEObject
Set ws = Sheets("Sheet1")
Set oo = ws.OLEObjects("Object 1")
oo.Verb xlVerbPrimary
End Sub
回答by maciej
The OP asked for a way of including parameters, which doesn't seem to be possible with the accepted solution. I implemented this a different way. This code extracts the file to the workbook's directory and then executes it.
OP 要求提供一种包含参数的方法,这在公认的解决方案中似乎是不可能的。我以不同的方式实现了这一点。此代码将文件提取到工作簿的目录中,然后执行它。
Sub saveAndRunFileExample()
ActiveSheet.OLEObjects(1).Copy
CreateObject("Shell.Application").Namespace(ActiveWorkbook.Path).Self.InvokeVerb "Paste"
Call Shell(ActiveWorkbook.Path & "\example.exe --parameter", vbNormalFocus)
End Sub