subject search

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to use Access to find all messages in a folder containing a
particular string, I am currently trying to use .RESTRICT, but it wont let me
use LIKE.

Any help appreciated.

Thanks
 
Restrict won't use SQL type syntax usually. It uses the =, >, <, >=, <= and
<> operators in addition to And and Or.

However, there's an undocumented trick you can use to use SQL type syntax
but then you have to use DAV syntax to refer to the property you want to
access. The trick is to preface the restriction with "@SQL = " and then add
your DASL restriction. For example:

Set colFiltered = colItems.Restrict("@SQL = urn:schemas:httpmail:subject
LIKE 'Foobar' ")

There are lists of the DASL property tags for various Outlook properties in
the Exchange SDK.
 
Ken

I have tried using this but I keep getting 'Condition is not valid' error
when I try to run it.
Code:
Set myolApp = CreateObject("Outlook.Application")
Set myNameSpace = myolApp.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(Fldr).Items
Set myMail = myFolder.Restrict("@SQL = urn:schemas:httpmail:subject LIKE
""QKM50629-1035""")

Thanks
 
Try using single quotes for your LIKE condition. In SQL syntax that's what
you do to surround a text string.
 
Ken

I have tried the following code but the same error occurs, I put Msgbox's in
to determine if this statement was the cause, I get the first one then the
error.
Code:
Set myolApp = CreateObject("Outlook.Application")
Set myNameSpace = myolApp.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox).Items
Fltr = "@SQL = urn:schemas:httpmail:subject LIKE 'QKM50629-1035'"
MsgBox "I am here"
Set myMail = myFolder.Restrict("@SQL = urn:schemas:mailheader:subject
LIKE 'QKM50629-1035'")
MsgBox "I have made it here"

Confused
 
Ken

I have also tried using 'Advanced Search' but it the search seems not to
end, the Editor reports running, I have waited upto 5 minutes with no result,
so I stopped it.

This code is a Class called ThisOutlookSession

Sub TestAdvancedSearchComplete()
Dim sch As Outlook.Search
Dim rsts As Outlook.Results
Dim i As Integer
blnSearchComp = False
Const strF As String = "urn:schemas:mailheader:subject = 'Test'"
Const strS As String = "Inbox"
Set sch = Application.AdvancedSearch(strS, strF)
While blnSearchComp = False
DoEvents
Wend
Set rsts = sch.Results
For i = 1 To rsts.Count
MsgBox rsts.Item(i).SenderName
Next
End Sub

This is a sub code
Public 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
 
That code would never end since you have an infinite loop testing for a
change in blnSearchComp that will never occur. In addition you are using sch
in the search complete event, where you should be using SearchObject.

The following code works nicely here for me for an advanced search, all the
code is in ThisOutlookSession:

Sub TestAdvancedSearchComplete()
Dim sch As Outlook.Search
Dim strF As String
Dim strS As String
Dim strTag As String

' search to include any substring that includes 'Chris'

strF = Chr(34) & "urn:schemas:httpmail:subject" & _
Chr(34) & " LIKE '%Chris%'"

strS = "Inbox"
strTag = "Search1"

Set sch = Application.AdvancedSearch(Scope:=strS, Filter:=strF, _
SearchSubFolders:=False, Tag:=strTag)

Set sch = Nothing
End Sub

Public Sub Application_AdvancedSearchComplete(ByVal SearchObject As Search)
Dim rsts As Outlook.Results

If (SearchObject.Tag = "Search1") Then
Set rsts = SearchObject.Results
MsgBox "Search1 returned " & rsts.Count & " items"
End If

Set rsts = Nothing
End Sub
 
It's a matter of where you have your double quotes. I rewrote your code and
changed the search condition to match something in my Inbox as follows, try
this:

Sub SQLSyntaxForRestriction()
Dim myolapp As Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim MyItems As Outlook.Items
Dim FilteredItems As Outlook.Items
Dim Fltr As String

Set myolapp = CreateObject("Outlook.Application")
Set myNameSpace = myolapp.GetNamespace("MAPI")

Set MyItems = myNameSpace.GetDefaultFolder(olFolderInbox).Items

Fltr = "@SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & "
LIKE '%Chris%'"

Set FilteredItems = MyItems.Restrict(Fltr)

MsgBox "I have made it here, returned items count = " &
FilteredItems.Count

Set myolapp = Nothing
Set myNameSpace = Nothing
Set MyItems = Nothing
Set FilteredItems = Nothing
End Sub
 
Is there a way to use this AdvanceSearch Method to find text in the body of
the MailItem?
 
Sure, use "urn:schemas:httpmail:textdescription" for the Body of an email.
In the case where you want to find a phrase within the body text just use
LIKE '%whatever%' and make sure to surround the search text with "%"
characters.

There's a great trick you can use to see what the DASL syntax for a property
would be, use the customize current view dialog. If you dig down to that
dialog there's a Filter button. Click that and in the Advanced tab set up
your filter. Then go to the SQL tab to see what that property would be in
DASL syntax.
 
Back
Top