Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Excel execute embedded .exe object

I have inserted an object into Excel. This object is an exe (a console application).

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?

like image 966
Nathan Cooper Avatar asked Sep 23 '14 12:09

Nathan Cooper


2 Answers

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.

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
like image 181
maciej Avatar answered Nov 01 '22 17:11

maciej


If you import .exe to Excel with these steps:

  1. Insert - Object
  2. Select tab: Create from File
  3. Browse for exe file
  4. Check on "Display as icon"

then you can write VBA subroutine/macro like (I used rectangle shaped object to execute macro by clicking it):

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
like image 39
Miki Avatar answered Nov 01 '22 16:11

Miki