代码之家  ›  专栏  ›  技术社区  ›  Waleed

如何最大限度地将打开的outlook电子邮件的焦点设置为前台?

  •  0
  • Waleed  · 技术社区  · 2 年前

    我正在使用以下代码打开一个电子邮件项目(有特定条件)。
    之后,我需要最大化打开的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
    

    感谢您的宝贵意见和回答。

    0 回复  |  直到 2 年前
        1
  •  0
  •   Dmitry Streblechenko    2 年前

    呼叫 MailItem.Display ,然后激活 Inspector 通过调用对象 Inspector.Activate . 检查员 可以从中检索对象 MailItem.GetInspector .

    需要记住的一点是,如果父进程不在前台,Windows将不会将窗口带到前台。您需要使用 AttachThreadInput 函数-请参阅 https://stackoverflow.com/a/17793132/332059

        2
  •  0
  •   Eugene Astafiev    2 年前

    您可以使用 SetForegroundWindow 方法,该方法将创建指定窗口的线程带到前台并激活该窗口。键盘输入指向窗口,并且为用户改变各种视觉提示。或者,您可以考虑使用 Activate 的方法 Explorer Inspector 类。

    要最大化窗口,可以使用 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值作为第二个参数,以最大化窗口。看见 How to minimize/maximize opened Applications 了解更多信息。

        3
  •  0
  •   FaneDuru    2 年前

    为了激活“ 打开的outlook电子邮件窗口 你需要“确定它的句柄”。为了做到这一点,你可以使用它的标题。

    1. 请使用下面的声明 标准模块 (在申报区):
     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
    

    上述变量是提供、保存和使用必要的窗口句柄(在注册表中/从注册表中)所必需的

    1. 适应 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
    

    请尝试并发送一些反馈。