How can I find outlook mail window by FindWindow..? RRS feed

  • Question

  • Private Declare Function FindWindow Lib "user32" _
      Alias "FindWindowA" (ByVal lpClassName As String, _
      ByVal lpWindowName As String) As Long

    Private Declare Function BringWindowToTop Lib "user32" _
    (ByVal hWnd As Long) As Long

    Dim WithEvents exp As Explorer
    Dim WithEvents mail As MailItem

    Private Sub Application_Startup()
        Set exp = Application.ActiveExplorer
    End Sub

    Private Sub exp_SelectionChange()
        If exp.Selection.Count = 1 Then
          If TypeName(exp.Selection.Item(1)) = "MailItem" Then
            Set mail = exp.Selection.Item(1)
          End If
        End If
    End Sub

    Private Sub mail_PropertyChange(ByVal Name As String)
        '//If Name = "UnRead" And mail.UnRead = False Then
        If mail.UnRead = False Then
            '//Preventing Mysterious Crash Error(Put Delay code)
            Dim T As Date
            Dim NextTime As Date
            T = Now
            NextTime = T + TimeValue("00:00:01")
            Do While Now < NextTime

            UserForm1.Show vbModeless
        End If

    'This for activating window
    Dim hWnd As Long
    hWnd = FindWindow("??????", ???????)
        If hWnd Then
              '//AppActivate ????
              BringWindowToTop hWnd
        End If

    End Sub

    Above are my code to float userform when open newly arrived mails.

    the problem is, when I click userform, mail window is hide.

    I want both new window are always on the top of window.

    How can I find window of newly arrived mail?

    Saturday, February 24, 2018 2:05 AM

All replies

  • Hi Truth,

    Thanks for your post.

    This forum is used to discuss about Outlook Object Model, your issue is more related with VBA function. 

    I will move it to the more related forum.

    Best Regards,

    Tao Zhou

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Tuesday, February 27, 2018 5:04 AM
  • I don't use Outlook but whenever I want to control/manipulate Windows I use AutoIT.
    Tuesday, February 27, 2018 10:09 PM