Public Sub OpenWindowsExplorer(sFullFolderPath As String)
On Error GoTo Err_Proc
Shell "C:\WINDOWS\explorer.exe """ & sFullFolderPath & "", vbNormalFocus
Exit_Proc:
Exit Sub
Err_Proc:
Call LogError_feo(Err, Err.Description, "modCommon @ OpenWindowsExplorer")
Resume Exit_Proc
End Sub
Call OpenWindowsExplorer("C:\XOM\SPTTemp")
#If VBA7 Then
Private Const SW_RESTORE As LongPtr = 9
Private Declare PtrSafe Function FindWindow Lib "USER32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Private Declare PtrSafe Function ShowWindow Lib "user32.dll" (ByVal hwnd As LongPtr, ByVal nCmdShow As LongPtr) As LongPtr
Private Declare PtrSafe Function BringWindowToTop Lib "USER32" (ByVal hwnd As LongPtr) As LongPtr
#Else
Private Const SW_RESTORE As Long = 9
Private Declare Function FindWindow Lib "USER32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function BringWindowToTop Lib "USER32" (ByVal hwnd As Long) As Long
#End If
Public Sub OpenWindowsExplorer(sFullFolderPath As String)
On Error GoTo Err_Proc
#If VBA7 Then
Dim THandle As LongPtr
#Else
Dim THandle As Long
#End If
Dim tmp As String, tmpArr() As String
tmpArr = Split(sFullFolderPath, "\", , vbTextCompare)
If UBound(tmpArr) = -1 Then
tmp = ""
Else
tmp = tmpArr(UBound(tmpArr))
End If
THandle = FindWindow(vbNullString, tmp)
If THandle = 0 Then
Shell "C:\WINDOWS\explorer.exe """ & sFullFolderPath & "", vbNormalFocus
Else
ShowWindow THandle, SW_RESTORE
BringWindowToTop THandle
End If
Exit_Proc:
Exit Sub
Err_Proc:
Call LogError_feo(Err, Err.Description, "modCommon @ OpenWindowsExplorer")
Resume Exit_Proc
End Sub
Sub Test()
Dim x As Integer
OpenWindowsExplorer "c:\AAA"
For x = 1 To 2000
DoEvents
Next x
OpenWindowsExplorer "c:\AAA"
End Sub
Looks like a delay between calls did the trick. Under normal circumstances (time lag between 1st and 2nd opening), the code should work without the delay.
'placed in a global module.
Public Const g_sLocalPath As String = "C:\XOM\SPTTemp\"
'placed in another global module.
Public Const g_sLocalAddPath As String = "EMRM Financials"
Call OpenWindowsExplorer(g_sLocalPath & g_sLocalAddPath)
THandle = FindWindow(vbNullString, tmp)
If it is different in any way, you'll get a 2nd window.
Option Compare Database
Option Explicit
'placed in a global module.
Public Const g_sLocalPath As String = "C:\AAA\BBB\"
'placed in another global module.
Public Const g_sLocalAddPath As String = "CCC CCC"
#If VBA7 Then
Private Const SW_RESTORE As LongPtr = 9
Private Declare PtrSafe Function FindWindow Lib "USER32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Private Declare PtrSafe Function ShowWindow Lib "user32.dll" (ByVal hwnd As LongPtr, ByVal nCmdShow As LongPtr) As LongPtr
Private Declare PtrSafe Function BringWindowToTop Lib "USER32" (ByVal hwnd As LongPtr) As LongPtr
#Else
Private Const SW_RESTORE As Long = 9
Private Declare Function FindWindow Lib "USER32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function BringWindowToTop Lib "USER32" (ByVal hwnd As Long) As Long
#End If
Sub Test()
'the public constants are defined in the declarations section above.
Call OpenWindowsExplorer(g_sLocalPath & g_sLocalAddPath)
' Dim x As Integer
' OpenWindowsExplorer "c:\AAA\BBB\CCC CCC"
' For x = 1 To 2000
' DoEvents
' Next x
' OpenWindowsExplorer "c:\AA"
End Sub
Public Sub OpenWindowsExplorer(sFullFolderPath As String)
On Error GoTo Err_Proc
#If VBA7 Then
Dim THandle As LongPtr
#Else
Dim THandle As Long
#End If
Dim x As Integer
Dim tmp As String, tmpArr() As String
' For x = 1 To 2000
' DoEvents
' Next x
tmpArr = Split(sFullFolderPath, "\", , vbTextCompare)
If UBound(tmpArr) = -1 Then
tmp = ""
Else
tmp = tmpArr(UBound(tmpArr))
End If
' For x = 1 To 2000
' DoEvents
' Next x
THandle = FindWindow(vbNullString, tmp)
Debug.Print tmp
If THandle = 0 Then
Shell "C:\WINDOWS\explorer.exe """ & sFullFolderPath & """", vbNormalFocus
Else
ShowWindow THandle, SW_RESTORE
BringWindowToTop THandle
End If
Exit_Proc:
Exit Sub
Err_Proc:
Call LogError_feo(err, err.Description, "modCommon @ OpenWindowsExplorer")
Resume Exit_Proc
End Sub
Sub LogError_feo(err, desc, text)
MsgBox err & vbCrLf & desc & vbCrLf & text
End Sub
If THandle = 0 Then
Shell "C:\WINDOWS\explorer.exe """ & sFullFolderPath & """", vbNormalFocus
Else
ShowWindow THandle, SW_RESTORE
BringWindowToTop THandle
End If
ShowWindow THandle, SW_RESTORE
BringWindowToTop THandle
THandle = FindWindow(vbNullString, tmp)
results in 0 the first time thru, but if I leave the Explorer window open, close the excel file, then reopen it and run Test() again, it results in a large number (handle number) so it does NOT =0.THandle = FindWindow(vbNullString, tmp)
please note image.
Also how can I change the above to allow for both 32 bit and 64 bit applications to run it without throwing an error?