我正在使用以下代碼打開一個電子郵件專案(具有特定條件)。
之后我需要最大化打開的 Outlook 電子郵件視窗并將焦點設定為前景。
Option Explicit
Option Compare Text
Public WithEvents MyItem As Outlook.MailItem
Public EventsDisable As Boolean
Private Sub Application_ItemLoad(ByVal Item As Object)
If EventsDisable = True Then Exit Sub
If Item.Class = olMail Then
Set MyItem = Item
End If
End Sub
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
End If
EventsDisable = False
End Sub
以下 Windows API 函式
#If Win64 Then
Private Declare PtrSafe Function SetForegroundWindow Lib "user32" _
(ByVal hWnd As LongPtr) As LongPtr
#Else
Private Declare Function SetForegroundWindow Lib "user32" _
(ByVal hWnd As Long) As Long
#End If
Public Sub Bring_to_front()
Dim setFocus As Long
setFocus = SetForegroundWindow(xxxxxxx.hWnd)
End Sub
感謝您提供任何有用的評論和答案。
uj5u.com熱心網友回復:
呼叫MailItem.Display,然后通過呼叫激活Inspector物件Inspector.Activate。Inspector可以從中檢索物件MailItem.GetInspector。
要記住的一件事是,如果父行程不在前臺,Windows 不會將視窗帶到前臺。您需要為此使用AttachThreadInput功能 - 請參閱https://stackoverflow.com/a/17793132/332059
uj5u.com熱心網友回復:
您可以使用SetForegroundWindow方法將創建指定視窗的執行緒帶到前臺并激活視窗。鍵盤輸入被定向到視窗,并且為用戶改變了各種視覺提示。或者,您可以考慮使用Explorer的Activate方法或Outlook 物件模型中的類。Inspector
要最大化視窗,您可以使用 Windows API 中的ShowWindow方法,這是 VBA 中可能的宣告:
Public Declare Function ShowWindow Lib "user32" _
(ByVal hwnd As Long, ByVal nCmdSHow As Long) As Long
private SW_MAXIMIZE as Long = 3;
private SW_MINIMIZE as Long = 6;
因此,您需要傳遞一個視窗句柄和 SW_MAXIMIZE 值作為第二個引數來最大化視窗。有關更多資訊,請參閱如何最小化/最大化打開的應用程式。
uj5u.com熱心網友回復:
為了激活“打開的 Outlook 電子郵件訊息視窗”,您需要“確定其句柄”。為此,您可以使用其標題。
- 請使用標準模塊頂部的下一個宣告(在宣告區域中):
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 請在同一標準模塊中復制下一個 API 函式:
#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
上述變數對于提供、保存和使用必要的視窗句柄是必需的(在/從注冊表中)
- 適應
myItem_Open下一種方式:
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 如果郵件視窗必須從另一個應用程式的 VBA 顯示在前臺,則上面的宣告和 API 函式也必須復制到模塊頂部,保留必要的(以下)子。
3.2復制下一個改編Sub并運行它(在Outlook中顯示必要的郵件視窗之后,當然......):
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
請嘗試一下并發送一些反饋。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/463726.html
