I'm trying to execute search on the Deleted Items folder but I get following error Run-time error '-2147023281 (8007064f)': The operation failed
on line Set Search = ...
. Code runs without problem on Inbox but fails to run on any other folder.
' Program.bas standard module
Option Explicit
Public Sub FindAndOpenMail()
Dim Query As String
' Construct filter. 0x0037001E represents Subject.
' ci_phrasematch is taken from Filtering Items Using Query Keywords (MS DOCS).
Query = "https://schemas.microsoft.com/mapi/proptag/0x0037001E" & _
" ci_phrasematch 'Inactive'"
Dim Search As Search
Set Search = Application.AdvancedSearch("Deleted Items", Query)
End Sub
CodePudding user response:
AdvancedSearch
required four parameters. Each of them build in a specific way. Your code tries it with only two parameters, which is wrong. Theoretically the third and fourth parameters are optional, but the last one is required for the code to work as it should.
In order to make it working it is not so simple as it looks... AdvancedSearch
works in a different thread and it is necessary for the code to know that the search has been completed.
Outlook exposes AdvancedSearchComplete
event which is triggered and it must be used, to change a boolean variable value, which to 'inform' the code to continue with the next steps.
I asked a question in a comment and according to your answer I can continue with a piece of working code. Since you try it in that way, it looks that you did not study too much the method in discussion, no offence...
Supposing that you try the code inside Outlook application and need only a solution for the code line returning the error, the first function parameter should be built as:
Dim delIt As Folder, sc As String 'the Scope parameter
Set delIt = Application.Session.GetDefaultFolder(olFolderDeletedItems)
sc = "'" & delIt.FolderPath & "'"
'then call the method as:
Set Search = Application.AdvancedSearch(sc, Query)
CodePudding user response:
When you specify search scope you would need to specify each folder path or name with apostrophes, for example:
Public m_SearchComplete As Boolean
Private Sub Application_AdvancedSearchComplete(ByVal SearchObject As Search)
If SearchObject.Tag = "MySearch" Then
m_SearchComplete = True
End If
End Sub
Sub TestSearchForMultipleFolders()
Dim Scope As String
Dim Filter As String
Dim MySearch As Outlook.Search
Dim MyTable As Outlook.Table
Dim nextRow As Outlook.Row
m_SearchComplete = False
'Establish scope for multiple folders
Scope = "'" & Application.Session.GetDefaultFolder( _
olFolderInbox).FolderPath _
& "','" & Application.Session.GetDefaultFolder( _
olFolderSentMail).FolderPath & "'"
'Establish filter
If Application.Session.DefaultStore.IsInstantSearchEnabled Then
Filter = Chr(34) & "urn:schemas:httpmail:subject" _
& Chr(34) & " ci_phrasematch 'Office'"
Else
Filter = Chr(34) & "urn:schemas:httpmail:subject" _
& Chr(34) & " like '%Office%'"
End If
Set MySearch = Application.AdvancedSearch( _
Scope, Filter, True, "MySearch")
While m_SearchComplete <> True
DoEvents
Wend
Set MyTable = MySearch.GetTable
Do Until MyTable.EndOfTable
Set nextRow = MyTable.GetNextRow()
Debug.Print nextRow("Subject")
Loop
End Sub
The parameters of the search, as specified by the Filter
argument of the AdvancedSearch
method, will return all items in the Inbox
and Sent Items
folders where the Subject
phrase-matches or contains "Office". The user's Inbox
and Sent Items
folders are specified as the scope of the search and the SearchSubFolders
property is set to true. However, only the first parameter is required, others are optional.