我已經設法通過使用 VBA 從 Excel 訪問 Outlook 中的 2 個檔案夾中的專案,但現在我想[email protected]在這兩個檔案夾中搜索電子郵件地址,我知道如何單獨搜索每個,但是一次,然后排序最近的一。我堅持的部分是如何同時查看兩個檔案夾。
我正在使用 Microsoft Office 2016
顯然,這條虛擬線不能解決問題:Set olJoinedFldr = olCleanUp olFldr
Private Sub CommandButton2_Click()
Dim olApp As Outlook.Application 'set app
Dim olNs As Object 'get namespace
Dim olFldr As Outlook.Folder 'to be the inbox
Dim olArchive As Outlook.Folder 'the archive folder
Dim olCleanUp As Outlook.Folder ' the archive subfolder we need
Dim olJoinedFldr As Object 'the to be made joined object to filter....
Dim olItems As Object 'filtered items based on search criteria
Dim olItemReply As Object 'the reply mail
Dim i As Long
Dim emailStr As String
Dim filter As String
Set olApp = CreateObject("Outlook.Application")
Set olNs = olApp.GetNamespace("MAPI")
Set olFldr = olNs.GetDefaultFolder(6) ' olFolderInbox
Set olArchive = olNs.Folders(CStr(olNs.Accounts.Item(1))) 'find email of current user
Set olCleanUp = olArchive.Folders("Archive").Folders("Cleanup") ' get the archive sub folder
Set olJoinedFldr = olCleanUp olFldr
Set emailStr = "[email protected]"
filter = "[SenderEmailAddress] = """ & emailStr & """" 'this is the email from person x we are searching for in the 2 folders
' from here on it is currently searching just 1 folder
Set olItems = olFldr.Items.Restrict(filter) 'filter the items
olItems.Sort "[ReceivedTime]", True 'sort by date
If olItems.Count > 0 Then
For i = 1 To olItems.Count
If olItems(i).Class = 43 Then
Set olItemReply = olItems(i).ReplyAll
With olItemReply
.HTMLBody = "<p Dear someone, <br><br></p>" & .HTMLBody
.Display
End With
Exit For
End If
Next
Else
' have code here to make a brand new email already
End If
Set olApp = Nothing
Set olNs = Nothing
Set olFldr = Nothing
Set olArchive = Nothing
Set olCleanUp = Nothing
Set olJoinedFldr = Nothing
Set olItems = Nothing
Set olItemReply = Nothing
Set i = Nothing
Set emailStr = Nothing
Set filter = Nothing
End Sub
uj5u.com熱心網友回復:
Search除非您使用創建物件,否則您無法搜索兩個(或更多)檔案夾Application.AdvancedSearch。即使這樣,它也是一個可以使用的 PITA - 搜索是異步的,您需要使用事件來確定搜索何時完成。
您最好一次搜索一個檔案夾并在代碼中組合結果(如有必要)。
uj5u.com熱心網友回復:
您需要使用類的AdvancedSearch方法Application。AdvancedSearch在 Outlook 中使用該方法的主要好處是:
- 搜索在另一個執行緒中執行。您不需要手動運行另一個執行緒,因為該
AdvancedSearch方法會在后臺自動運行它。 - 可以在任何位置(即超出某個檔案夾的范圍)搜索任何專案型別:郵件、約會、日歷、便箋等。和
Restrict/方法可以應用于特定的集合(參見 Outlook 中類的Find屬性)。FindNextItemsItemsFolder - 完全支持 DASL 查詢(自定義屬性也可用于搜索)。您可以在 MSDN中的過濾文章中閱讀有關此內容的更多資訊。為了提高搜索性能,如果為商店啟用了即時搜索,則可以使用即時搜索關鍵字(參見
IsInstantSearchEnabledStore 類的屬性)。 Stop您可以使用類的方法隨時停止搜索程序Search。
在 Outlook中的高級搜索中以編程方式閱讀有關此方法的更多資訊:C#、VB.NET文章。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/449917.html
