shawnmei 发表于 2015-9-14 08:10:13

Outlook add-ins webcasts讲座代码之四(高级搜索)

Partial Public Class ThisApplicationClass ThisApplication
    'outlook的菜单栏
    Dim _menuBar As Office.CommandBar = Nothing

    '创建菜单按钮
    Dim _topMenu As Office.CommandBarPopup = Nothing

    '创建生成搜索文件夹的菜单
    Dim _menuSearch As Office.CommandBarButton

    Private Sub CreateSearch()Sub CreateSearch()
      '定义一个Search对象
      Dim mailSearch As Outlook.Search
      Dim strFolder As String
      Dim strSearch As String

      '获得收件箱文件夹
      Dim inbox As Outlook.MAPIFolder = Me.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
      '获得已发送邮件文件夹
      Dim sendbox As Outlook.MAPIFolder = Me.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail)

      '定义搜索范围
      strFolder = "SCOPE ('shallow traversal of """ & _
                  sendbox.FolderPath & """ ', 'shallow traversal of """ & _
                  inbox.FolderPath & """ ')"
      '定义查询条件
      strSearch = "urn:schemas:httpmail:fromemail LIKE '%fastid@21cn.net%' OR urn:schemas:httpmail:displayto LIKE '%fastid@21cn.net%'"

      mailSearch = Me.AdvancedSearch(strFolder, strSearch, True, "MySearch")
      mailSearch.Save("邮件查询")

    End Sub
    Private Sub _menuSearch_Click()Sub _menuSearch_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean)
      CreateSearch()
    End Sub

    Private Sub CreateMenus()Sub CreateMenus()
      _menuBar = Me.ActiveExplorer().CommandBars.ActiveMenuBar

      If (Not (_menuBar Is Nothing)) Then

            Dim _control As Office.CommandBarControl

            '寻找学员信息管理菜单
            For Each _control In _menuBar.Controls
                If (_control.Caption = "学员信息管理") Then
                  _topMenu = _control
                End If
            Next

            If (Not (_topMenu Is Nothing)) Then
                '添加创建搜索的菜单
                _menuSearch = _topMenu.Controls.Add(Office.MsoControlType.msoControlButton, _
                                        Type.Missing, _
                                        Type.Missing, _
                                        Type.Missing, _
                                        True)
                _menuSearch.Caption = "查询邮件"
                _menuSearch.Visible = True

                '添加菜单的点击事件处理
                AddHandler _menuSearch.Click, AddressOf _menuSearch_Click
            End If
      End If
    End Sub
End Class
页: [1]
查看完整版本: Outlook add-ins webcasts讲座代码之四(高级搜索)