Nathan
Nathan

Reputation: 6531

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?

Upvotes: 5

Views: 8954

Answers (2)

maciej
maciej

Reputation: 464

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

Upvotes: 4

Miki
Miki

Reputation: 2517

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

Upvotes: 1

Related Questions