Closing a Shell Window through VBA

goofy78270

Well-known Member
Joined
May 16, 2007
Messages
555
I have a VBA that opens a Shell window to access another application. In an effort to clean it up, I am looking to close this shell window after the VBA is ran. I have looked through google with no luck so any help would be great.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
goofy78270,

Check this out - ShellAndWait - Waiting For A Shelled Process To Complete:
http://www.cpearson.com/excel/ShellAndWait.htm

Have a great day,
Stan
This is a great site, but it does not accomplish what I am looking for. I am looking for a way to manually terminate the shell application from the VBA and not implement a wait until the user closes the application which is what this is doing.
It calls the Shell command, Opens the process, and then uses WaitForSingleObject to return when the shell'd program terminates.
 
Upvote 0
I found a solution. I think this might help others looking to do the same thing so I am posting it.


Code:
The code below shows you how to start an application (in this case, notepad) using the Shell statement, and then close it again by sending it the WM_CLOSE message. Please note that not all applications pay an attention to this message! You will have to try it first.


Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Const WM_CLOSE = &H10
Private lNotepadhWnd As Long
Private Sub cmdStartNotepad_Click()
    Call Shell("notepad", vbNormalFocus)
    DoEvents
    lNotepadhWnd = GetForegroundWindow
End Sub

Private Sub cmdCloseNotepad_Click()
    Call SendMessage(lNotepadhWnd, WM_CLOSE, 0, 0)
End Sub

Thanks to James Crowley for this bit of code at http://www.developerfusion.co.uk/show/146/
 
Upvote 0

Forum statistics

Threads
1,215,580
Messages
6,125,654
Members
449,245
Latest member
PatrickL

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