如何使用VBA在打开的Outlook窗口中循环?

时间:2018-03-23 17:52:17

标签: vba outlook outlook-vba

我经常打开多个电子邮件项目,有些是我编写的尚未发送的,有些是我收到的,但我还没有结束但是我正在引用。

我想要做的是快速浏览所有打开的窗口以找到我正在寻找的窗口。

在Excel中,我创建了一个宏来循环浏览Excel文档的选项卡。

Sub PreviousSheet()
    On Error Resume Next
    ActiveSheet.Previous.Select
End Sub

Sub NextSheet()
    On Error Resume Next
    ActiveSheet.Next.Select 
End Sub

在Outlook中,我如何使用VBA循环打开窗口?

更新

Sub test()

    Dim olApp As Outlook.Application
    Set olApp = GetOutlookApp()

    'I think this is how to loop through the open items?
    For i = olApp.Inspectors.Count To 1 Step -1
        Set olItem = olApp.Inspectors.Item(i).CurrentItem
        olItem.Select 'How do I set focus?
    Next i

End Sub

Function GetOutlookApp() As Outlook.Application
     ' returns reference to native Application object
    Set GetOutlookApp = Outlook.Application
End Function

1 个答案:

答案 0 :(得分:0)

Sub GetPreviousOpenItem()

    Set MainWindow = Application.ActiveExplorer

    Dim olApp As Outlook.Application
    Set olApp = GetOutlookApp()

    If olApp.ActiveInspector Is Nothing Then
        Exit Sub
    End If

    ActiveInspectorIndex = GetIndexOfActiveInspector(olApp, olApp.ActiveInspector)

    If ActiveInspectorIndex - 1 > 0 Then
        Dim PreviousInspector As Inspector
        Set PreviousInspector = olApp.Inspectors(ActiveInspectorIndex - 1)
        olApp.Inspectors(ActiveInspectorIndex - 1).Display
    Else
        olApp.Inspectors(olApp.Inspectors.Count).Display
    End If

    MainWindow.Activate

End Sub

Sub GetNextOpenItem()

    Set MainWindow = Application.ActiveExplorer

    Dim olApp As Outlook.Application
    Set olApp = GetOutlookApp()

    If olApp.ActiveInspector Is Nothing Then
        Exit Sub
    End If

    ActiveInspectorIndex = GetIndexOfActiveInspector(olApp, olApp.ActiveInspector)

    If ActiveInspectorIndex + 1 <= olApp.Inspectors.Count Then
        Dim NextInspector As Inspector
        Set NextInspector = olApp.Inspectors(ActiveInspectorIndex + 1)
        NextInspector.Display
    Else
        olApp.Inspectors(1).Display
    End If

    MainWindow.Activate

End Sub

Function GetIndexOfActiveInspector(olApp, CurrentItem) As Integer

    CurrentItem = olApp.ActiveInspector

    For i = 1 To olApp.Inspectors.Count

        Dim Inspector
        Set Inspector = olApp.Inspectors.Item(i)
        Set olItem = Inspector.CurrentItem

        If olItem Is CurrentItem Then
            GetIndexOfActiveInspector = i
            Exit Function
        End If

    Next i

    MainWindow.Activate

End Function
Function GetOutlookApp() As Outlook.Application
     ' returns reference to native Application object
    Set GetOutlookApp = Outlook.Application
End Function