Open cmd.exe in excel 365 vba

Viewed 29

I want to open cmd.exe in excel 365 vba. I used to open shell in excel 2013 but it seems not to work in excel 365.

I tried this code and works for calc.exe, ... but not for cmd.exe. Does it need a special parameter/permission to run from vba.

Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As LongPtr, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As LongPtr

Sub ShellExec()
Dim strFile As String
Dim strAction As String
Dim lngErr As LongPtr

' Edit this:
strFile = "notepad.exe"  ' the file you want to open/etc.
strAction = "OPEN"  ' action might be OPEN, NEW or other, depending on what you need to do

lngErr = ShellExecute(0, strAction, strFile, "", "", 0)

' optionally, add code to test lngErr

End Sub

Kind regards, w.

1 Answers

use SW_SHOW = 5 as last parameter

Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As LongPtr, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As LongPtr

Sub ShellExec()
Dim strFile As String
Dim strAction As String
Dim lngErr As LongPtr

' Edit this:
strFile = "cmd.exe"  ' the file you want to open/etc.
strAction = "OPEN"  ' action might be OPEN, NEW or other, depending on what you need to do

lngErr = ShellExecute(0, strAction, strFile, "", "", 5)

' optionally, add code to test lngErr

End Sub
Related