vba 在 Excel 文件中嵌入 EXE 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8296057/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:33:53  来源:igfitidea点击:

Embed EXE file in the Excel file

windowsvbaexcel-vbaexcel-2003excel

提问by David Heffernan

I use:

我用:

retVal = Shell("program.EXE " & filename, vbNormalFocus)

To execute a program need for my excel spreadsheet.

执行程序需要我的 excel 电子表格。

Is it possible to embed the EXE file in the excel file itself?

是否可以将 EXE 文件嵌入到 excel 文件本身中?

And how would I execute it then?

那么我将如何执行它?

Ideias:

理念:

1 - Some kind of a bin2strfunction to turn binary to string (so I can store it in the program as a variable and a str2bin(the oposite)

1 - 某种将二进制转换为字符串的bin2str函数(因此我可以将其作为变量和str2bin(相反)存储在程序中)

2 - I read something about OLE Control(that you can embed it there), but I really don't know where to start on this one

2 - 我读了一些关于OLE Con​​trol 的东西(你可以把它嵌入那里),但我真的不知道从哪里开始这个

回答by David Heffernan

Here's an outline solution that avoids OLE:

这是一个避免 OLE 的大纲解决方案:

  1. Create a hidden worksheet.
  2. Use a base 64 encoded to convert the exe to text.
  3. Store that text in worksheet cells on the hidden worksheet. Since there is a limit on the number of characters in a cell (32,767) you will need to break the string into chunks.
  1. 创建一个隐藏的工作表。
  2. 使用 base 64 编码将 exe 转换为文本。
  3. 将该文本存储在隐藏工作表上的工作表单元格中。由于单元格中的字符数有限制 (32,767),因此您需要将字符串分成块。

Obviously you'll need to reverse this procedure when you want to save and execute the exe file.

显然,当您想要保存和执行 exe 文件时,您需要反转此过程。

回答by user3540526

You can do this by using: Insert > Object and then selecting 'Create from File'.

您可以使用以下方法执行此操作:插入 > 对象,然后选择“从文件创建”。

To add it to your sheet using VBA:

要使用 VBA 将其添加到您的工作表中:

Dim o As OLEObject
Set o = ActiveSheet.OLEObjects.Add(Filename:="C:\program.exe")

Then this is the command to execute program.exe:

然后这是执行program.exe的命令:

o.Verb Verb:=xlPrimary

Not sure how to pass arguments to it, however (e.g. your filename).

但是,不确定如何将参数传递给它(例如您的filename)。

Note: Untrusted applications prompt a warning when you run them.

注意:不受信任的应用程序在您运行时会提示警告。