Avatar of Senniger1
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
OutlookVB ScriptVisual Basic Classic

Avatar of undefined
Last Comment
Martin Liss

8/22/2022 - Mon
wshark83

try something like the code below from --> http://msdn.microsoft.com/en-us/library/aa220071(v=office.11).aspx:

Public sch As Outlook.Search

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

alternatively and app is already developed here http://soroush.secproject.com/blog/projects/exceladvancedsearchapplication/
Senniger1

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!
wshark83

Const strF As String = "urn:schemas:httpmail:subject = 'Corp'"

will do that you can change it to '(Corp'
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Senniger1

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.
ASKER CERTIFIED SOLUTION
wshark83

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Senniger1

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: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")

and commenting out line 6
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Senniger1

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: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 sincerely appreciate your help!
Module1.bas
Class1.cls
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
SOLUTION
Chris Bottomley

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Senniger1

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!
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 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

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Senniger1

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!
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.

Chris
Senniger1

ASKER
Thank you anyway.  I do appreciate your time.

Can anyone else assist me in this matter?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Chris Bottomley

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:

https://www.experts-exchange.com/questions/25904435/DASL-filter-in-Outlook-2003-Username.html

Chris
Martin Liss

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.