Senniger1
asked on
VBA Code for Advanced Find in Outlook
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)
Can anyone help me with this code.
Thanks in advance!
AdvanceFind.jpg
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
Can anyone help me with this code.
Thanks in advance!
AdvanceFind.jpg
ASKER
Forgive me for asking, but what part of your code covers the CONTAINS part of my request.
I need where SUBJECT CONTAINS the text (Corp
Thanks!
I need where SUBJECT CONTAINS the text (Corp
Thanks!
Const strF As String = "urn:schemas:httpmail:subj ect = 'Corp'"
will do that you can change it to '(Corp'
will do that you can change it to '(Corp'
ASKER
I admit I'm limited in my knowledge here, but you codes looks like you have the subject equaling '(Corp' and I need the subject to contain '(Corp'.
Doesn't the part "subject = '(Corp' indicate equals or does this also cover CONTAINS.
Doesn't the part "subject = '(Corp' indicate equals or does this also cover CONTAINS.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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:ht tpmail:sub ject), "(corp") (SYNTAX ERROR)
Line 7 Set sch = Application.AdvancedSearch (strS, strF, , "Search1")
Line 8 End Sub
Please advise, thanks!
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:ht
Line 7 Set sch = Application.AdvancedSearch
Line 8 End Sub
Please advise, thanks!
ok try changing line 4 to:
Const strF As String = InStr(lcase(urn:schemas:ht tpmail:sub ject), "(corp")
and commenting out line 6
Const strF As String = InStr(lcase(urn:schemas:ht
and commenting out line 6
ASKER
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:s ubject"), "(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!
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:
Line 5 Const strS As String = "Tasks"
Line 6 Set sch = Application.AdvancedSearch
Line 7 End Sub
Thanks!
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:h ttpmail:su bject)),'( corp'"
Const strS As String = "Tasks"
Set sch = Application.AdvancedSearch (strS, strF, , "Search1")
End Sub
Sub TestAdvancedSearchComplete
Dim rsts As Outlook.Results
Dim i As Integer
Const strF As String = "InStr(lcase(urn:schemas:h
Const strS As String = "Tasks"
Set sch = Application.AdvancedSearch
End Sub
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_AdvancedSearch Complete(B yVal 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:h ttpmail:su bject)),'( 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 "TestAdvancedSearchComplet e" 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 sincerely appreciate your help!
Module1.bas
Class1.cls
I put this in a Class Module...
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
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:h
Const strS As String = "Tasks"
Set sch = Application.AdvancedSearch
End Sub
I'm getting a "Variable not defined on the following line of your code...
Set sch = Application.AdvancedSearch
So I added adding the following:
Dim sch As Outlook.Search
When running "TestAdvancedSearchComplet
I sincerely appreciate your help!
Module1.bas
Class1.cls
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!
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!
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
Once you are happy with the functionality we can normally redirect the data in VBA wherever you want it.
Chris
The same code but this time to use a msgbox is ...
Sub taskFilterCorp()
Dim olFolder As Object
Dim filteredItems As Object
Dim folderItems As Object
Dim sorteditems As Object
Dim strFilter As String
Dim itm As Object
Dim 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 str
End Sub
ASKER
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!
Again, thank you so much!
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.
Chris
Chris
ASKER
Thank you anyway. I do appreciate your time.
Can anyone else assist me in this matter?
Can anyone else assist me in this matter?
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:subje ct" 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:
https://www.experts-exchange.com/questions/25904435/DASL-filter-in-Outlook-2003-Username.html
Chris
urn:schemas:httpmail:subje
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:
https://www.experts-exchange.com/questions/25904435/DASL-filter-in-Outlook-2003-Username.html
Chris
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
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/