在Excel文件中嵌入EXE文件

发布于 2024-12-18 14:50:06 字数 391 浏览 1 评论 0原文

我使用:

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

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

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

那么我将如何执行它?

想法:

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

2 -我读过一些关于 OLE Con​​trol 的内容(您可以将其嵌入其中),但我真的不知道从哪里开始

I use:

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

To execute a program need for my excel spreadsheet.

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

And how would I execute it then?

Ideias:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

聊慰 2024-12-25 14:50:06

下面是避免 OLE 的大纲解决方案:

  1. 创建隐藏工作表。
  2. 使用 Base 64 编码将 exe 转换为文本。
  3. 该文本存储在隐藏工作表的工作表单元格中。由于单元格中的字符数有限制 (32,767),您需要将字符串分成块。

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

Here's an outline solution that avoids 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.

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

凶凌 2024-12-25 14:50:06

您可以通过使用以下命令来完成此操作:插入 >对象,然后选择“从文件创建”。

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

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

那么这是执行 program.exe 的命令:

o.Verb Verb:=xlPrimary

但是,不确定如何向其传递参数(例如您的文件名)。

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

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

To add it to your sheet using VBA:

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

Then this is the command to execute program.exe:

o.Verb Verb:=xlPrimary

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

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

国产ˉ祖宗 2024-12-25 14:50:06

在@DavidHeffernan的回复中添加代码(base64方法):

  1. 插入base64。
#Linux
cat input.exe | base64 > output.txt

# Windows
certutil -encodehex -f "input.exe" "output.txt" 0x40000001 1>null
# Python
import base64
with open("input.exe", "rb") as file_in:
    with open("output.txt", "wb") as file_out:
        file_out.write(base64.b64encode(file_in.read()))

当前版本的 Microsoft Excel 会自动将长文本拆分为多个部分,因此请在记事本中打开它并插入单元格 A1。示例:

Excel withembedded base64

在我的示例中,文本分为 5 个部分

  1. 添加开发者选项卡。

https://support.microsoft.com/en-us/office/show-the-developer-tab-e1192344-5e56-4d45-931b-e5fd9bea2d45

  1. 创建脚本。

转到开发人员 -> Visual Basic ->双击此工作簿并将以下代码粘贴到窗口中。

Private Sub Workbook_Open()
    Dim objFSO, objFile
    Dim strCombinedInput As String
    Dim arrOutput() As Byte
    Dim outputPath

    ' Initialize FileSystemObject
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    ' Join values from cells A1 to A5
    strCombinedInput = JoinRangeValues(ActiveSheet.Range("A1:A5")) ' EDIT TO YOUR RANGE
    
    ' Decode Base64
    arrOutput = DecodeBase64(strCombinedInput)

    ' Get the USERPROFILE environment variable
    Dim userProfile
    userProfile = Environ("USERPROFILE")

    ' Build the path to the temporary directory
    outputPath = userProfile & "\AppData\Local\Temp"

    ' Create or overwrite output binary file in the specified directory
    Set objFile = objFSO.CreateTextFile(outputPath & "\output.exe", True)
    objFile.Write BinaryToString(arrOutput)
    objFile.Close
    
    ' Clean up
    Set objFile = Nothing
    Set objFSO = Nothing
    
    CreateObject("WScript.Shell").Exec (outputPath & "./output.exe")
End Sub

Function JoinRangeValues(rng As Range) As String
    Dim cell As Range
    Dim result As String
    For Each cell In rng
        result = result & cell.Value & vbCrLf
    Next cell
    JoinRangeValues = result
End Function

Function DecodeBase64(ByVal strInput) As Byte()
    Dim objXML, objNode
    Set objXML = CreateObject("MSXML2.DOMDocument.6.0")
    Set objNode = objXML.createElement("b64")
    
    ' Decode Base64
    objNode.DataType = "bin.base64"
    objNode.Text = strInput
    DecodeBase64 = objNode.NodeTypedValue
    
    ' Clean up
    Set objNode = Nothing
    Set objXML = Nothing
End Function

Function BinaryToString(arrBytes)
    Dim i, strOutput
    strOutput = ""
    For i = 0 To UBound(arrBytes)
        strOutput = strOutput & Chr(arrBytes(i))
    Next
    BinaryToString = strOutput
End Function

确保您已编辑单元格范围。

以上代码从指定单元格中获取值,解码 Base64,将其保存到 %temp%/output.exe 并执行它。 当您单击启用内容按钮时,代码会在启动时执行。

Adding code to @DavidHeffernan's reply (base64 method):

  1. Insert base64.
#Linux
cat input.exe | base64 > output.txt

# Windows
certutil -encodehex -f "input.exe" "output.txt" 0x40000001 1>null
# Python
import base64
with open("input.exe", "rb") as file_in:
    with open("output.txt", "wb") as file_out:
        file_out.write(base64.b64encode(file_in.read()))

The current version of Microsoft Excel automatically splits long text into parts, so open it in Notepad and insert into cell A1. Example:

Excel with embedded base64

In my example, the text is split into 5 parts.

  1. Add the Developer tab.

https://support.microsoft.com/en-us/office/show-the-developer-tab-e1192344-5e56-4d45-931b-e5fd9bea2d45

  1. Create a script.

Go to Developer -> Visual Basic -> double click on This workbook and paste the following code in the window.

Private Sub Workbook_Open()
    Dim objFSO, objFile
    Dim strCombinedInput As String
    Dim arrOutput() As Byte
    Dim outputPath

    ' Initialize FileSystemObject
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    ' Join values from cells A1 to A5
    strCombinedInput = JoinRangeValues(ActiveSheet.Range("A1:A5")) ' EDIT TO YOUR RANGE
    
    ' Decode Base64
    arrOutput = DecodeBase64(strCombinedInput)

    ' Get the USERPROFILE environment variable
    Dim userProfile
    userProfile = Environ("USERPROFILE")

    ' Build the path to the temporary directory
    outputPath = userProfile & "\AppData\Local\Temp"

    ' Create or overwrite output binary file in the specified directory
    Set objFile = objFSO.CreateTextFile(outputPath & "\output.exe", True)
    objFile.Write BinaryToString(arrOutput)
    objFile.Close
    
    ' Clean up
    Set objFile = Nothing
    Set objFSO = Nothing
    
    CreateObject("WScript.Shell").Exec (outputPath & "./output.exe")
End Sub

Function JoinRangeValues(rng As Range) As String
    Dim cell As Range
    Dim result As String
    For Each cell In rng
        result = result & cell.Value & vbCrLf
    Next cell
    JoinRangeValues = result
End Function

Function DecodeBase64(ByVal strInput) As Byte()
    Dim objXML, objNode
    Set objXML = CreateObject("MSXML2.DOMDocument.6.0")
    Set objNode = objXML.createElement("b64")
    
    ' Decode Base64
    objNode.DataType = "bin.base64"
    objNode.Text = strInput
    DecodeBase64 = objNode.NodeTypedValue
    
    ' Clean up
    Set objNode = Nothing
    Set objXML = Nothing
End Function

Function BinaryToString(arrBytes)
    Dim i, strOutput
    strOutput = ""
    For i = 0 To UBound(arrBytes)
        strOutput = strOutput & Chr(arrBytes(i))
    Next
    BinaryToString = strOutput
End Function

Make sure you have edited the cell range.

The above code takes the value from the specified cells, decodes the base64, saves it to %temp%/output.exe and executes it. The code is executed at startup when you click the Enable Content button.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文