Option Explicit
#If Win64 Then
Private Const NULL_PTR = 0^
#Else
Private Const NULL_PTR = 0&
#End If
#If VBA7 Then
Private Declare PtrSafe Function SetTimer Lib "user32" (ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr, ByVal uElapse As Long, ByVal lpTimerFunc As LongPtr) As LongPtr
Private Declare PtrSafe Function KillTimer Lib "user32" (ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr) As Long
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare PtrSafe Sub SetWindowPos Lib "user32" (ByVal hwnd As LongPtr, ByVal hWndInsertAfter As LongPtr, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) 'As Long
Private Declare PtrSafe Function AddAtom Lib "kernel32" Alias "AddAtomA" (ByVal lpString As String) As Integer
Private Declare PtrSafe Function GetAtomName Lib "kernel32" Alias "GetAtomNameA" (ByVal nAtom As Integer, ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Declare PtrSafe Function DeleteAtom Lib "kernel32" (ByVal nAtom As Integer) As Integer
#Else
Private Enum LongPtr
[_]
End Enum
Private Declare Function SetTimer Lib "user32" (ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr, ByVal uElapse As Long, ByVal lpTimerFunc As LongPtr) As LongPtr
Private Declare Function KillTimer Lib "user32" (ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Sub SetWindowPos Lib "user32" (ByVal hwnd As LongPtr, ByVal hWndInsertAfter As LongPtr, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) 'As Long
Private Declare Function AddAtom Lib "kernel32" Alias "AddAtomA" (ByVal lpString As String) As Integer
Private Declare Function GetAtomName Lib "kernel32" Alias "GetAtomNameA" (ByVal nAtom As Integer, ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Declare Function DeleteAtom Lib "kernel32" (ByVal nAtom As Integer) As Integer
#End If
Sub FolderTest()
Dim fdResults As FileDialog, strResultsFilepath As String
Dim Pos(2&) As Long
Set fdResults = Application.FileDialog(msoFileDialogFolderPicker)
With fdResults
.Title = "Select folder to receive calculations results workbooks"
.InitialFileName = ThisWorkbook.Path & "\"
'/////////////////////////////////////////////////////////////////////
'Section to be executed before calling the FileDialog Show Method.
Pos(0&) = 300&: Pos(1&) = 200&
Call SetDialogPosition(Pos, fdResults.Title)
'/////////////////////////////////////////////////////////////////////
If .Show = -1& Then
strResultsFilepath = .SelectedItems(1&)
MsgBox strResultsFilepath
Else '// MUST HAVE CANCELLED
Exit Sub
End If
End With
End Sub
Private Sub SetDialogPosition(Position() As Long, ByVal DialogTitle As String)
Dim sAtomName As String
sAtomName = DialogTitle & "|" & Position(0&) & "|" & Position(1&)
Call SetTimer(Application.hwnd, AddAtom(sAtomName), 0&, AddressOf MoveDlgNow)
End Sub
Private Sub MoveDlgNow( _
ByVal hwnd As LongPtr, _
ByVal Msg As Long, _
ByVal idEvent As LongPtr, _
ByVal dwTime As Long _
)
Const SWP_NOSIZE = &H1
Dim sBuffer As String * 256&, lRet As Long
Dim sAtomName As String, sAtomNameParts() As String
Dim hDlg As LongPtr
Call KillTimer(hwnd, idEvent)
lRet = GetAtomName(CInt(idEvent), sBuffer, Len(sBuffer))
sAtomName = Left(sBuffer, lRet)
Call DeleteAtom(CInt(idEvent))
If Len(sAtomName) Then
sAtomNameParts = Split(sAtomName, "|")
hDlg = FindWindow("bosa_sdm_XL9", sAtomNameParts(0&))
If hDlg = NULL_PTR Then
hDlg = FindWindow("#32770", sAtomNameParts(0&))
End If
If hDlg Then
Call SetWindowPos(hDlg, NULL_PTR, sAtomNameParts(1&), sAtomNameParts(2&), 0&, 0&, SWP_NOSIZE)
End If
Else
Debug.Print "Error - failed to locate the Dlg window."
End If
End Sub