如何最大化并将重点放在打开的Outlook电子邮件的前景上?

发布于 2025-01-23 12:32:00 字数 1147 浏览 2 评论 0 原文

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

感谢您的任何有用的评论和答案。

I am using below code to open an email item (with specific conditions).
I need after that to maximize the opened outlook email window and set focus for it to be foreground.

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

the following Windows API function

#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

thanks for any useful comments and answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

○闲身 2025-01-30 12:32:00

调用 mailItem.display ,然后通过调用 boce> Inspector> Inspector.activate 来激活 Inspector 对象。 Inspector 可以从 mailItem.getInspector 中检索对象。

要记住的一件事是,如果父进程不在前景中,窗户不会将窗户带到前景。您需要使用 attactthreadinput 函数 - 请参见 332059

Call MailItem.Display, then activate the Inspector object by calling Inspector.Activate. Inspector object can be retrieved from MailItem.GetInspector.

One thing to keep in mind is that Windows will not bring a window to the foreground if the parent process is not in the foreground. You would need to use AttachThreadInput function for that - see https://stackoverflow.com/a/17793132/332059

南渊 2025-01-30 12:32:00

您可以使用 activate Inspector 从Outlook对象模型类。

为了最大化窗口,您可以使用 showwindow 从Windows API中的方法,这是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_Maximimime值则作为第二个参数以最大化窗口。请参阅如何最小化/最大化打开的应用程序以获取更多信息。

You can use the SetForegroundWindow method which brings the thread that created the specified window into the foreground and activates the window. Keyboard input is directed to the window, and various visual cues are changed for the user. Alternatively you may consider using the Activate method of the Explorer or Inspector classes from the Outlook object model.

To maximize the window you could use the ShowWindow method from Windows API, here is a possible declaration in 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;

So, you need to pass a window handle and the SW_MAXIMIZE value as the second parameter to maximize the window. See How to minimize/maximize opened Applications for more information.

尹雨沫 2025-01-30 12:32:00

为了激活“ 打开的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

中复制下一个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.a请在同一标准模块 变量对于供应,保存和使用必要的窗口句柄(在注册表中/从注册表中)

  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

请尝试并发送一些反馈。

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.

  1. 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)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文