Close windows explorer

AJIT SINGH

New Member
Joined
Mar 15, 2010
Messages
2
Need a VB code in excel to close the windows explorer. My macro performs the following function.
  • Opens the window explorer of the desired path on the basis of excel sheet.
  • Then open the specific excel file from the folder opened at step1.
  • Updates the spreadsheet and then closes it.
  • At this step I need to close the explorer window (opened at step 1).
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hi and welcome to MrExcel.

Getting a reference to the HWND or PID of a given Explorer Window is for some obscure reason very difficult. I know several methods of doing this with other Processes/Windows but none those methods work for Explorer windows. I would love to see if this can be done. I'll keep trying and if anything comes up i'll post it here.

Regards.
 
Upvote 0
I've done some research and some testing but there doesn't seem to be a simple way of doing this.

One cannot use the PID returned by the Shell command and subsequently terminate the Explorer.exe Process via the TerminateProcess API using the returned PID because all the launched Explorer windows run in the same Process as that of the main System Explorer. In other words all new launched Explorer Windows are just different open windows of the same Explorer Process.

Even walking through all the Top level Windows via the EnumWindows API and even catching the creation of the Explorer Window via a CBT hook didn't work !

So the only remaining option is to try and get the hwnd of our target Explorer Window using the FindWindow Function and try to close it programmatically. However another problem comes up with this because each Explorer Window seems to have a different Class name depending on the desired path !

Try experimenting with the EXPLORER_CLASSNAME Constante in the example code below and see if it works by changing the Const from "CabinetWClass" to "ExploreWClass" or vice versa:

Code:
Option Explicit
 
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByRef lParam As Any) As Long
 
Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
 
Private Const SC_CLOSE = &HF060&
Private Const WM_SYSCOMMAND = &H112
 
Private Const EXPLORER_CLASSNAME As String = "CabinetWClass"
[COLOR=seagreen]'Private Const EXPLORER_CLASSNAME As String = "ExploreWClass"[/COLOR]
 
Sub Test()
 
    Dim lExplhwnd As Long
    Dim lTimer As Long
 
    Shell "explorer.exe C:\", vbMaximizedFocus
 
    [COLOR=seagreen]'======================================[/COLOR]
   [COLOR=seagreen]'this loop is just for illustration pupose.[/COLOR]
    [COLOR=seagreen]'remove this temporary loop and replace it[/COLOR]
   [COLOR=seagreen]'with your Macro stuff here....[/COLOR]
    lTimer = Timer
    Do
        DoEvents
    Loop Until Timer - lTimer >= 3 [COLOR=seagreen]'wait 3 Secs[/COLOR].
   [COLOR=seagreen]'======================================[/COLOR]
 
    lExplhwnd = FindWindow(EXPLORER_CLASSNAME, vbNullString)
 
    If lExplhwnd Then
        SendMessage lExplhwnd, WM_SYSCOMMAND, SC_CLOSE, ByVal 0&
    End If
 
End Sub

The above worked for me on WIN XP . Not sure if this would work on different platforms.

Regards.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,877
Members
452,949
Latest member
Dupuhini

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