Solved ShellExecute Outlook.exe (1 Viewer)

LarryE

Well-known member
Local time
Today, 02:50
Joined
Aug 18, 2021
Messages
1,090
I have the following code:
Code:
Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As LongPtr, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As LongPtr
Option Compare Database
Option Explicit
Dim result As LongPtr
result = ShellExecute(0, "open", "OUTLOOK.EXE", vbNullString, vbNullString, SW_SHOWMINIMIZED)
which correctly opens Outlook but will not minimize it to the taskbar. Does anyone know why not?
 
For one thing, I think need to define constants:

Code:
' Show command constants
Private Const SW_HIDE As Long = 0
Private Const SW_SHOWNORMAL As Long = 1
Private Const SW_SHOWMINIMIZED As Long = 2
Private Const SW_SHOWMAXIMIZED As Long = 3

or just use the numeric value.


And another thing, why are Dim and result lines not within a procedure? A procedure does work for me. Your posted code will not compile.
 
Last edited:
Try the below option. It should open outlook without activating it so it should remain minimized.

Code:
Const SW_SHOWMINNOACTIVE = 7
result = ShellExecute(0, "open", "OUTLOOK.EXE", vbNullString, vbNullString, SW_SHOWMINNOACTIVE)
 
I think because need to define the constants:

Code:
' Show command constants
Private Const SW_HIDE As Long = 0
Private Const SW_SHOWNORMAL As Long = 1
Private Const SW_SHOWMINIMIZED As Long = 2
Private Const SW_SHOWMAXIMIZED As Long = 3

or just use the numeric value.
Already tried that. ShellExecute will not recognize any of the SW_ commands or any of the numeric values. I also tried using the Outlook hWnd window to get/set the minimize option and that will not work either. I am using Outlook classic.
 
I tested my above method and it works for me -- MS Access 365 64bit, Windows 11. If you haven't tried option 7 (SW_SHOWMINNOACTIVE), give it a try. SW_SHOWMINIMIZED doesn't work as it activates the application, which for Outlook results in the window not remaining minimized.
 
Private Declare PtrSafe Function GetActiveWindow Lib "user32" () As LongPtr
result = ShellExecute(GetActiveWindow, "open", "OUTLOOK.EXE", vbNullString, vbNullString, SW_SHOWMINIMIZED)
did not work either. Opened but will not minimize.
Firstly, that is not the syntax that link used?
They use ShellExecute GetActiveWindow(), though TBH I do not know the difference, so would copy exactly as is.


Another post states Shell will do it.
Please read whole thread, particularly see post #11
 
Option 7 works for me but not option 2

EDIT: Retested using Excel, Word & PowerPoint. In each case, both options open the app minimized to the taskbar. Outlook is the exception.
 
Last edited:
@DaveMD suggestion works for me.

Code:
Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As LongPtr, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As LongPtr
Private Const SW_SHOWMINNOACTIVE = 7
Option Compare Database
Option Explicit

Sub OpenOL()
Dim result As LongPtr
result = ShellExecute(0, "open", "OUTLOOK.EXE", vbNullString, vbNullString, SW_SHOWMINNOACTIVE)
End Sub
 
This code worked for me?
Code:
Private Sub Command1_Click()
Shell "C:\Program Files\Internet Explorer\IEXPLORE.EXE [url]http://www.smallwebpage.com[/url]", vbMinimizedNoFocus
End Sub
 
I tested my above method and it works for me -- MS Access 365 64bit, Windows 11. If you haven't tried option 7 (SW_SHOWMINNOACTIVE), give it a try. SW_SHOWMINIMIZED doesn't work as it activates the application, which for Outlook results in the window not remaining minimized.
Code:
Const SW_SHOWNOACTIVATE As Long = 7
result = ShellExecute(0, "open", "OUTLOOK.EXE", vbNullString, vbNullString, 7)

Worked very well. I didn't know about 7. I thought it was 2.
Thank you very much. Solved.
 
Showminimized in that other thread was set as 6?
I don't know but here is the most recent list I could find:
Code:
Private Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hWnd As LongPtr, ByVal nCmdShow As Long) As Long
Public Const SW_HIDE As Long = 0
Public Const SW_SHOWNORMAL As Long = 1
Public Const SW_SHOWMINIMIZED As Long = 2
Public Const SW_SHOWMAXIMIZED As Long = 3
Public Const SW_SHOWNOACTIVATE As Long = 4
Public Const SW_SHOW As Long = 5
Public Const SW_MINIMIZE As Long = 6
Public Const SW_SHOWMINNOACTIVE As Long = 7
Public Const SW_SHOWNA As Long = 8
Public Const SW_RESTORE As Long = 9
Public Const SW_SHOWDEFAULT As Long = 10
Public Const SW_FORCEMINIMIZE As Long = 11
7 was the one that worked for my purpose.
 

Users who are viewing this thread

Back
Top Bottom