In order to activate "the opened outlook email message window" you need to "determine its handle". In order to do that you may use its caption.
- Please, use the next declarations on top of a standard module (in the declarations area):
Public Const MyApp As String = "myOutlook", Sett As String = "Settings", wHwnd As String = "Wind_Hwnd" 'changed to be `Public` and keeping the handle
1.a Please copy the next API functions in the same standard module:
#If Win64 Then
Public Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Public Declare PtrSafe Function SetForegroundWindow Lib "user32" _
(ByVal hwnd As LongPtr) As LongPtr
Public Declare PtrSafe Function ShowWindow Lib "user32" _
(ByVal hwnd As LongPtr, ByVal nCmdSHow As Long) As Long
#Else
Public Declare Function FindWindow Lib "User32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function SetForegroundWindow Lib "user32" _
(ByVal hWnd As Long) As Long
Public Declare Function ShowWindow Lib "user32" _
(ByVal hwnd As Long, ByVal nCmdSHow As Long) As Long
#End If
The above variables are necessary to supply, save and use the necessary window handle (in/from Registry)
- Adapt
myItem_Open
in the next way:
Private Sub myItem_Open(Cancel As Boolean)
EventsDisable = True
If MyItem.Subject = "Auto Plan" And Application.ActiveExplorer.CurrentFolder.Name = "MyTemplate" Then
'Code to maximize the opened outlook email window and set focus for it to be foreground
#If Win64 Then
Dim meHwnd As LongPtr
#Else
Dim meHwnd As Long
#End If
meHwnd = FindWindow(vbNullString, MyItem.GetInspector.Caption) 'find the necessary window handle
SaveSetting MyApp, Sett, wHwnd, CStr(meHwnd) 'memorize it, converted to string
End If
EventsDisable = False
End Sub
3.1 If the mail window must be shown in foreground from VBA of another application, the declarations and API functions from above, must be also copied on top of the module keeping the necessary (following) sub.
3.2 Copy the next adapted Sub
and run it (after showing the necessary mail window in Outlook, of course...):
Sub Bring_to_front()
Dim winHwnd As String, i As Long
winHwnd = GetSetting(MyApp, Sett, wHwnd, "No Value")
If winHwnd <> "No Value" Then
#If Win64 Then
Dim mailWindHwnd As LongPtr
mailWindHwnd = CLngPtr(winHwnd)
#Else
Dim mailWindHwnd As Long
mailWindHwnd = CLng(winHwnd)
#End If
SetForegroundWindow mailWindHwnd
ShowWindow mailWindHwnd, 3
End If
End Sub
Please, try it and send some feedback.