Outlook Sleep无需挂GUI

时间:2016-06-27 13:42:09

标签: vba outlook outlook-vba

方案

我正在尝试检查用户是否在循环中使用了Outlook窗口(并且计算机未锁定)(大约30秒)。但是,我不希望GUI在这个实例中不可用,并且因此使用以下代码来尝试使用。我想知道用户在计算机未锁定时写入消息需要多长时间。 (EX。在写电子邮件和计算机锁定的过程中)

代码

Private Sub Wait(ByVal DurationMS As Long)
   Dim EndTime As Long
   EndTime = GetTickCount + DurationMS
   Do While EndTime > GetTickCount
       Sleep 1
       DoEvents
   Loop
End Sub

Function CheckUnlock()
    If Check_If_Locked = "Unlocked" Then
        UnlockStatus = "Unlocked"
        Call InsertText
        Wait 30000
        Call AAA
        Call CheckUnlock
    Else
        myFile = CreateObject("WScript.Shell").specialfolders("Desktop") & "\outlook-info.csv"

        Open myFile For Append As #1
        nowTime = Format(Now(), "hh:mm:ss")
        Print #1, "COMPUTER LOCKED:" + "," + CStr(nowTime)
        Close #1

        Wait 300000
        Call CheckUnlock
    End If
End Function

Function Check_If_Locked() As String
    Dim p_lngHwnd As Long
    Dim p_lngRtn As Long
    Dim p_lngErr As Long
    p_lngHwnd = OpenDesktop(lpszDesktop:="Default", dwFlags:=0, fInherit:=False, dwDesiredAccess:=DESKTOP_SWITCHDESKTOP)
    If p_lngHwnd = 0 Then
        system = "Error"
    Else
        p_lngRtn = SwitchDesktop(hDesktop:=p_lngHwnd)
        p_lngErr = Err.LastDllError

        If p_lngRtn = 0 Then
            If p_lngErr = 0 Then
                system = "Locked"
            Else
                system = "Error"
            End If
        Else
            system = "Unlocked"
        End If
        p_lngHwnd = CloseDesktop(p_lngHwnd)
    End If
    Check_If_Locked = system
End Function

问题

当GUI响应时,它会在文件夹更改时挂起,我知道它是因为的睡眠功能,但是,我不确定为什么文件夹是唯一受此影响的文件夹。电子邮件回复得很好。

提前致谢,欢迎任何帮助。

1 个答案:

答案 0 :(得分:2)

您可以使用Outlook的explorer object来接收event次通知。每次主Outlook窗口获得或失去焦点时,下面的示例都会弹出一个消息框。锁定电脑将消除焦点。

示例

Private WithEvents e As Explorer        ' Explorer object, represents main window.


Private Sub Application_Startup()
' Register for events.

    Set e = Application.ActiveExplorer
End Sub

Private Sub Application_Quit()
' Unregister.

    Set e = Nothing
End Sub

Private Sub e_Activate()
' Fired when outlook is selected.

    MsgBox "Active"
End Sub

Private Sub e_Deactivate()
' Fired when Outlook loses the focus, including on lock.

    MsgBox "Not Active"
End Sub

将自己的代码添加到activate和deactivate事件中,以进行自定义。

以下是Microsoft文档的link,您可能会对此有所帮助。