Open "explorer exe" in VBA and define the size and the position of the window

29Eric29

New Member
Joined
Jun 22, 2018
Messages
1
Hello

I'm trying to open 4 windows of "explorer.exe" (windows explorer) application and I would like to define their position and size.

I found some code which works perfectly with notepad !

I've tried to modify it ...to make it work with "explorer.exe"

I've replaced :
np_retval = Shell("C:\windows\notepad.exe", vbNormalFocus)

by :

np_retval = Shell("C:\windows\explorer.exe", vbNormalFocus)


But it's not as simple !! (I'm not an expert at all..sorry)

Here is the code for notepad.exe. If anyone can help me to adapt it for "explorer.exe" (for w7 & w10) ??

Thanks a lot !!!

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Option Explicit

Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long

Public Const GW_HWNDNEXT As Long = 2
Public Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwprocessid As Long) As Long

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Sub tile1()
Dim retval As Long, np_retval As Long

np_retval = Shell("C:\windows\notepad.exe", vbNormalFocus)
retval = MoveWindow(GetWinHandle(np_retval), 0, 0, 950, 550, 1) ' Application.hwnd ' X Y largeur hauteur

np_retval = Shell("C:\windows\notepad.exe", vbNormalFocus)
retval = MoveWindow(GetWinHandle(np_retval), 950, 0, 950, 550, 1) ' Application.hwnd ' X Y largeur hauteur

np_retval = Shell("C:\windows\notepad.exe", vbNormalFocus)
retval = MoveWindow(GetWinHandle(np_retval), 950, 550, 950, 550, 1) ' Application.hwnd ' X Y largeur hauteur

np_retval = Shell("C:\windows\notepad.exe", vbNormalFocus)
retval = MoveWindow(GetWinHandle(np_retval), 0, 550, 950, 550, 1) ' Application.hwnd ' X Y largeur hauteur

End Sub

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Function ProcIDFromWnd(ByVal hwnd As Long) As Long
Dim idProc As Long

' Get PID for this HWnd
GetWindowThreadProcessId hwnd, idProc
ProcIDFromWnd = idProc

End Function

Function GetWinHandle(hInstance As Long) As Long
Dim tempHwnd As Long

' Grab the first window handle that Windows finds:
tempHwnd = FindWindow(vbNullString, vbNullString)

' Loop until you find a match or there are no more window handles:
Do Until tempHwnd = 0
' Check if no parent for this window
If GetParent(tempHwnd) = 0 Then
' Check for PID match
If hInstance = ProcIDFromWnd(tempHwnd) Then
' Return found handle
GetWinHandle = tempHwnd
' Exit search loop
Exit Do
End If
End If

' Get the next window handle
tempHwnd = GetWindow(tempHwnd, GW_HWNDNEXT)
Loop
End Function
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I was searching for the exact same method you are looking for. Your code works wonderfully in my Windows 10 Excel 2016 system with one small change. I found that opening the "explorer.exe" to a defined folder makes your code work like a charm. Thank you for posting it. It certainly solved my dilemna. Here is the very simple change I made.

VBA Code:
np_retval = Shell("explorer.exe" & " " & "C:\Files\PDF-Files", vbNormalFocus)

Your code did not establish a folder for the file explorer to open. At least it works for me.
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,164
Members
453,021
Latest member
Justyna P

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