executing an exe file from macro

ashishprem

New Member
Joined
Feb 24, 2006
Messages
17
Hi, I am trying to execute an exe file by calling it from user form in excel but its not working. Its a dos mode exe file. while using the same code when i call notepad.exe it works perfect. any help will be really appreciated. The code which I am uning is as


<CODE>


Code:
Option Explicit
 
Private Declare Function ShellExecute _
Lib "shell32.dll" _
Alias "ShellExecuteA" ( _
ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) _
As Long
 
Private Declare Function GetDesktopWindow _
Lib "user32" () _
As Long
 
Private Const SW_HIDE As Long = 0
Private Const SW_NORMAL As Long = 1
Private Const SW_SHOW As Long = 5
Private Const SW_MAXIMIZE As Long = 3
Private Const SW_MINIMIZE As Long = 6
 
---------------------------------------------------------------------------------------
 
Public Function fnShellOperation(strFilePath As String, _
Optional strOperation As String, _
Optional nShowCmd As Double) As Long
 
Dim hWndDesk As Long
 
hWndDesk = GetDesktopWindow()
 
If Len(strOperation) = 0 Then strOperation = "Open"
If nShowCmd = Null Then nShowCmd = SW_MAXIMIZE
 
'// Failure when >0 or <=32
fnShellOperation = ShellExecute(hWndDesk, strOperation, strFilePath, 0, 0, nShowCmd)
If fnShellOperation <= 32 Then
MsgBox "Couldn't " & strOperation & " " & strFilePath & vbCrLf & vbCrLf & _
"Error:= " & fnShellErr(fnShellOperation)
End If
 
'// OK check IF there was an Association Error
If fnShellOperation = 31 Then
'// OK Ask user if they want to Open it using another program
If MsgBox(strOperation & " Using another Application", vbYesNo) = vbYes Then
Shell "rundll32.exe shell32.dll,OpenAs_RunDLL " & strFilePath, vbNormalFocus
End If
End If
End Function
 
'---------------------------------------------------------------------------------------
Public Function fnShellErr(Ret As Long) As String
 
Select Case Ret
'// Typical Errors
Case 0: fnShellErr = "The operating system is out of memory or resources."
Case Is = 2: fnShellErr = "The specified FILE was not found."
Case Is = 3: fnShellErr = "The specified PATH was not found."
Case Is = 5: fnShellErr = "The operating system denied access to the specified file."
Case Is = 8: fnShellErr = "There was not enough memory to complete the operation."
Case Is = 11: fnShellErr = "The .EXE file is invalid (non-Win32 .EXE or error in .EXE image)."
Case Is = 26: fnShellErr = "A sharing violation occurred."
Case Is = 27: fnShellErr = "The filename association is incomplete or invalid."
Case Is = 28: fnShellErr = "The DDE transaction could not be completed because the request timed out."
Case Is = 29: fnShellErr = "The DDE transaction failed."
Case Is = 30: fnShellErr = "The DDE transaction could not be completed because other DDE transactions were being processed."
Case Is = 31: fnShellErr = "There is no application associated with the given filename extension."
Case Is = 32: fnShellErr = "The specified dynamic-link library was not found."
Case Else: fnShellErr = "*UNDEFINED* Error"
End Select
 
End Function
 
 
Private Sub CommandButton4_Click()
Dim Ret As Long
 
'// Substitute here your Doc full path
Ret = fnShellOperation("C:\windows\notepad.exe", "Open", SW_MAXIMIZE)
Ret = fnShellOperation("C:\test\application.exe", "Open", SW_MAXIMIZE)
End Sub


</CODE>
 
Last edited by a moderator:

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi!

Just insert the following command line to execute exe file (change the exe file name accordingly):


Shell "C:\Temp\Test.exe"

Thanks
Shamsuddeen
 
Upvote 0
Hi!

Just insert the following command line to execute exe file (change the exe file name accordingly):


Shell "C:\Temp\Test.exe"

Thanks
Shamsuddeen
 
Upvote 0

Forum statistics

Threads
1,215,450
Messages
6,124,912
Members
449,195
Latest member
Stevenciu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top