I'm currently using Outlook 2010, but my question should work for Outlook 2003.
I need the VBA code for an ADVANCED FIND in Outlook for TASKS where SUBJECT CONTAINS "(Corp". See attachment for a screenshot of my search.
I'm thinking I would use the following and I believe I can cover the scope, but I don't know how to enter the FILTER part. Also I don't think I would need to search sub folders.
Set objSearch = Application.AdvancedSearch(Scope, Filter, SearchSubFolders, Tag)
Private Sub Application_AdvancedSearchComplete(ByVal SearchObject As Search)
Dim rsts As Outlook.Results
If (SearchObject.Tag = "Search1") Then
Set rsts = sch.Results
MsgBox "Search1 returned " & rsts.Count & " items"
End If
End Sub
Sub TestAdvancedSearchComplete()
Dim rsts As Outlook.Results
Dim i As Integer
Const strF As String = "urn:schemas:httpmail:subject = 'Corp'"
Const strS As String = "Tasks"
Set sch = Application.AdvancedSearch(strS, strF, , "Search1")
End Sub
Thanks for your help so far. I think we're heading in the right direction.
In running your code I get a SYNTAX ERROR on lines 4 and 6 below.
Line 1 Sub TestAdvancedSearchComplete()
Line 2 Dim rsts As Outlook.Results
Line 3 Dim i As Integer
Line 4 Const strF As String (SYNTAX ERROR)
Line 5 Const strS As String = "Tasks"
Line 6 strF = InStr(lcase(urn:schemas:httpmail:subject), "(corp") (SYNTAX ERROR)
Line 7 Set sch = Application.AdvancedSearch(strS, strF, , "Search1")
Line 8 End Sub
Please advise, thanks!
wshark83
ok try changing line 4 to:
Const strF As String = InStr(lcase(urn:schemas:httpmail:subject), "(corp")
Now I get errors on Line 4 and Line 6 (see below).
Line 1 Sub TestAdvancedSearchComplete()
Line 2 Dim rsts As Outlook.Results
Line 3 Dim i As Integer
Line 4 Const strF As String = InStr(LCase$("urn:schemas:httpmail:subject"), "(corp") (COMPILE ERROR: CONSTANT EXPRESSION REQUIRED)
Line 5 Const strS As String = "Tasks"
Line 6 Set sch = Application.AdvancedSearch(strS, strS, , "Search1") (SCH - Variable not defined)
Line 7 End Sub
Thanks!
wshark83
try this.. can you provide me with your sample file?
Sub TestAdvancedSearchComplete()
Dim rsts As Outlook.Results
Dim i As Integer
Const strF As String = "InStr(lcase(urn:schemas:httpmail:subject)),'(corp'"
Const strS As String = "Tasks"
Set sch = Application.AdvancedSearch(strS, strF, , "Search1")
End Sub
Senniger1
ASKER
I've provided the files you requested. Here's everything you've given me so far including the changes.
I put this in a Class Module...
Private Sub Application_AdvancedSearchComplete(ByVal SearchObject As Search)
Dim rsts As Outlook.Results
If (SearchObject.Tag = "Search1") Then
Set rsts = sch.Results
MsgBox "Search1 returned " & rsts.Count & " items"
End If
End Sub
This part is the macro I run...
Sub TestAdvancedSearchComplete()
Dim sch As Outlook.Search
Dim rsts As Outlook.Results
Dim i As Integer
Const strF As String = "InStr(lcase(urn:schemas:httpmail:subject)),'(corp'"
Const strS As String = "Tasks"
Set sch = Application.AdvancedSearch(strS, strF, , "Search1")
End Sub
I'm getting a "Variable not defined on the following line of your code...
Set sch = Application.AdvancedSearch(strS, strF, , "Search1")
So I added adding the following:
Dim sch As Outlook.Search
When running "TestAdvancedSearchComplete" I am still getting a Run-time error: The operation failed. When I debug it and hover over the "sch" it indicated "sch = Nothing".
I'm open for another way to handle my request so I tried your filter.
There were no errors and I believe it worked, but nothing really happened. I didn't get a results window with the results of my search.
What should be happening after I run this code?
Thanks so much!
Chris Bottomley
For the moment, (for the simplicity) the outputs are directed to the immediate window of the VBE, (ctrl + G) to display it.
Once you are happy with the functionality we can normally redirect the data in VBA wherever you want it.
Chris
Chris Bottomley
The same code but this time to use a msgbox is ...
Sub taskFilterCorp()Dim olFolder As ObjectDim filteredItems As ObjectDim folderItems As ObjectDim sorteditems As ObjectDim strFilter As StringDim itm As ObjectDim str As String Set olFolder = Application.Session.GetDefaultFolder(olFolderTasks) Set folderItems = olFolder.Items strFilter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " like " & "'%" & "corp" & "%'" Set filteredItems = olFolder.Items.Restrict(strFilter) If filteredItems.Count = 0 Then str = "No such tasks found" Else For Each itm In filteredItems str = str & itm.Subject & vbCrLf Next End If MsgBox strEnd Sub
Okay the output is absulutely perfect. I just now need the output to be in a filtered tasklist or something so they can open the tasks as they need.
Again, thank you so much!
Chris Bottomley
Okay, what I didn't make clear is this code makes a selection for use within VBA rather than a displayed collection in the explorer ... that brings you back to the advanced search and I apologise for the distraction.
Hmmm, I have assumed the help you were receiving on advanced search would meet your needs. Given the lack of progress there I implemented it on a pc and found that like filters that I went on about the advanced search returns a collection for dynamic analysis BUT does not modify the displayed items in the explorer. The wildcard element was via:
urn:schemas:httpmail:subject" LIKE '%corp%'
If you do require the explorer display to reflect the selected items then it can be done via the filter mechanism within outlook ... with a bit of work as it isn't quite so straightforward as a collection within VBA.
For an idea of where we will have to go to implement the DASL filter then see the solution to:
Public sch As Outlook.Search
Private Sub Application_AdvancedSearch
Dim rsts As Outlook.Results
If (SearchObject.Tag = "Search1") Then
Set rsts = sch.Results
MsgBox "Search1 returned " & rsts.Count & " items"
End If
End Sub
Sub TestAdvancedSearchComplete
Dim rsts As Outlook.Results
Dim i As Integer
Const strF As String = "urn:schemas:httpmail:subj
Const strS As String = "Tasks"
Set sch = Application.AdvancedSearch
End Sub
alternatively and app is already developed here http://soroush.secproject.com/blog/projects/exceladvancedsearchapplication/