Option Explicit Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long Private Const INFINITE = &HFFFF Private Declare Function ShellExecuteEx Lib "shell32.dll" (Prop As SHELLEXECUTEINFO) As Long Private Type SHELLEXECUTEINFO cbSize As Long fMask As Long Hwnd As Long lpVerb As String lpFile As String lpParameters As String lpDirectory As String nShow As Long hInstApp As Long lpIDList As Long lpClass As String hkeyClass As Long dwHotKey As Long hIcon As Long hProcess As Long End Type Private Const SEE_MASK_NOCLOSEPROCESS = &H40 Private Const SEE_MASK_DOENVSUBST = &H200 Private Const SEE_MASK_FLAG_NO_UI = &H400 ' -------------------------------------------------------------------------------- ' Launches the ShellExecuteEx function and waits for the generated process to exit ' - sVerb "edit", "explore", "find", "open", "print", "properties" ' ' - sFile String that specifies the name of the file or object on which ' ShellExecuteEx will perform the action specified by the lpVerb ' parameter. ' ' - sParameters String that contains the application parameters. ' The parameters must be separated by spaces. ' If the sFile parameter specifies a document file, lpParameters ' should be "". ' ' -sDirectory String that specifies the name of the working directory. ' If this string is "", the current directory is used as the ' working directory. ' ' -lShow See SW_ value list on ' https://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx ' Value 0 is SW_HIDE ' Value 10 is SW_SHOWDEFAULT ' ' - Return value TRUE if successful; otherwise, FALSE. ' Call GetLastError or refer to Err.DllLastError for extended ' error information. ' ' ' This example applies when mixing file prints and Excel Sheet.PrintOut ' to keep the printing order in the spool. In asynchroneous mode, this order ' is not guaranteed especially when printing many files and Excel sheets: ' ' bRetVal = ShellExecuteExSync("Print", "D:\Documents\monfichier.pdf") ' ' ' This example is the standard demo case with Notepad.exe: ' ' MsgBox "Before call" ' bRetVal = ShellExecuteExSync("Open", "Notepad.Exe", "monfichier.txt", _ ' "D:\Documents", 10) ' MsgBox "After call" ' ' ------------------------------------------------------------------------------ Function ShellExecuteExSync(ByVal sVerb As String, _ ByVal sFile As String, _ Optional ByVal sParameters As String, _ Optional ByVal sDirectory As String, _ Optional ByVal lShow As Long) As Boolean Dim lRet As Long Dim bRet As Boolean Dim Prop As SHELLEXECUTEINFO With Prop .cbSize = Len(Prop) .fMask = SEE_MASK_NOCLOSEPROCESS + SEE_MASK_DOENVSUBST + SEE_MASK_FLAG_NO_UI .Hwnd = 0& .lpVerb = sVerb .lpFile = sFile .lpParameters = sParameters 'Null string if .lpDirectory = sDirectory .nShow = lShow End With bRet = ShellExecuteEx(Prop) If bRet Then lRet = WaitForSingleObject(Prop.hProcess, INFINITE) ShellExecuteExSync = bRet End Function