MailItem Find Method question

Status
Not open for further replies.
S

Salad

Using the Find method to find a Subject, I have a couple of questions.

If the Subject is "Test", the following code finds the email. If the

subject is "Re: Test", it does not. I have to remove the prefix. Why

is that?

If the subject is null, my code doesn't find the email. I figure

someone can send an email without a subject. Any idea on correcting it?

If Not IsNull(strSubject) Then

Set objMail = _

objNS.GetDefaultFolder(6).Items.Find("[Subject] = """ & _

strSubject & """")

Else

Set objMail = _

objNS.GetDefaultFolder(6).Items.Find("[Subject] Is Null")

End If

I have a question on finding the email by date as well. I pass a date

field to the function. It converts the date time to a string then

searches. Out of 10 email times passed to it, if found only 1. Any

idea whay the code below fails the majority of the time?

strTime = Format(datTime, "m/d/yyyy h:nn AMPM")

Set objMail = objNS.GetDefaultFolder(6).Items.Find("[ReceivedTime]= """

& strTime & """")
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
If strSubject is declared as a String type, it never will be Null. Instead

use IsEmpty, or even better just:

If str="" then...

Actually you'd need the LIKE operator in the filter, but that's not

supported. So you'd have to loop through all the collection, and check each

item with

If item.subject like "*whatever*" Then ...

More flexible, and a lot faster, are the Restriction* objects from the

Redemption (www.dimastr.com)

Best regards

Michael Bauer

Am Sun, 21 Feb 2010 15:28:08 -0800 schrieb Salad:


> Using the Find method to find a Subject, I have a couple of questions.
> If the Subject is "Test", the following code finds the email. If the
> subject is "Re: Test", it does not. I have to remove the prefix. Why
> is that?

> If the subject is null, my code doesn't find the email. I figure
> someone can send an email without a subject. Any idea on correcting it?

> If Not IsNull(strSubject) Then
> Set objMail = _
> objNS.GetDefaultFolder(6).Items.Find("[Subject] = """ & _
> strSubject & """")
> Else
> Set objMail = _
> objNS.GetDefaultFolder(6).Items.Find("[Subject] Is Null")
> End If

> I have a question on finding the email by date as well. I pass a date
> field to the function. It converts the date time to a string then
> searches. Out of 10 email times passed to it, if found only 1. Any
> idea whay the code below fails the majority of the time?

> strTime = Format(datTime, "m/d/yyyy h:nn AMPM")
> Set objMail = objNS.GetDefaultFolder(6).Items.Find("[ReceivedTime]= """
> & strTime & """")
 
D

Dmitry Streblechenko

Under the hood, Outlook searches on the PR_NORMALIZED_SUBJECT (which does

not include the prefix), not PR_SUBJECT.

Where does the strSubject come from? Do you actually store it? Or does the

user type it?

Dmitry Streblechenko (MVP)

-

"Salad" <salad@oilandvinegar.com> wrote in message

news:LO2dnaTQZ-MYXBzWnZ2dnUVZ_u2dnZ2d@earthlink.com...
> Using the Find method to find a Subject, I have a couple of questions. If
> the Subject is "Test", the following code finds the email. If the subject
> is "Re: Test", it does not. I have to remove the prefix. Why is that?

> If the subject is null, my code doesn't find the email. I figure someone
> can send an email without a subject. Any idea on correcting it?

> If Not IsNull(strSubject) Then
> Set objMail = _
> objNS.GetDefaultFolder(6).Items.Find("[Subject] = """ & _
> strSubject & """")
> Else
> Set objMail = _
> objNS.GetDefaultFolder(6).Items.Find("[Subject] Is Null")
> End If

> I have a question on finding the email by date as well. I pass a date
> field to the function. It converts the date time to a string then
> searches. Out of 10 email times passed to it, if found only 1. Any idea
> whay the code below fails the majority of the time?

> strTime = Format(datTime, "m/d/yyyy h:nn AMPM")
> Set objMail = objNS.GetDefaultFolder(6).Items.Find("[ReceivedTime]= """ &
> strTime & """")
 
S

Salad

Dmitry Streblechenko wrote:


> Under the hood, Outlook searches on the PR_NORMALIZED_SUBJECT (which does
> not include the prefix), not PR_SUBJECT.
> Where does the strSubject come from? Do you actually store it? Or does the
> user type it?
>


In Access, one can do a File/Import/GetExternalData and link an inbox,

sent, etc folder and it's now a table, similar to an Excel spreadsheet.

So I'm using info from the inbox in my testing.

One of the fields is "Subject" that contains the entire subject line

"Re: Test". Another field is "Subject Prefix" with just the "Re:", and

"Normalized Subject" that contains "Test"

Besides the subject fields , there's also the

received/created/lastmodified columns, the email size, sender name, and

a few other fields like the body.

I thought it'd be relatively simple to find the email.

Works like a champ on the normalized subject as you noted.

I can'r find the email if the subject line is null.

I can't find the email based on any datetime stamp.

Works like a champ on message size.

But I figure there can be one or more emails with the same subject line,

same message size, or same datetimestamp. But all three? I don't think so.

Since the .Find method works fine on the subject and size fields, I'm

obviously passing the wrong filter for a null (blank) subject and for

the date.

I've tried, and failed on

> Find("IsEmpty([Subject]")

> Find("IsNull([Subject]")

> Find("[Subject] = ''")

and for dates

'Set objMail = .Find("[ReceivedTime]= '1/1/2010 3:34:00 AM'")

and Jan 1, 2010, January 1,2010, no seconds, no AM, just the date,

nothing works. Single quotes, double quotes, it doesn't seem to matter.

Based on the above, do you spot anything obvious that is in error? Or

other tests I could make? Or a link to compare my code to for finding

an email?
 
D

Dmitry Streblechenko

Searching by subject is a bad idea, to put it mildly :) What happens if you

have multiple messsages with the same subject?

Use the EntryID property and open the item using using

Application.Session.GetItemFromID. There is no reason to search.

Dmitry Streblechenko (MVP)

-

"Salad" <salad@oilandvinegar.com> wrote in message

news:sZCdnUnqUfV6Wh_WnZ2dnUVZ_omdnZ2d@earthlink.com...
> Dmitry Streblechenko wrote:
>
> > Under the hood, Outlook searches on the PR_NORMALIZED_SUBJECT (which
> > does not include the prefix), not PR_SUBJECT.
> > Where does the strSubject come from? Do you actually store it? Or does
> > the user type it?
> >


> In Access, one can do a File/Import/GetExternalData and link an inbox,
> sent, etc folder and it's now a table, similar to an Excel spreadsheet. So
> I'm using info from the inbox in my testing.

> One of the fields is "Subject" that contains the entire subject line "Re:
> Test". Another field is "Subject Prefix" with just the "Re:", and
> "Normalized Subject" that contains "Test"

> Besides the subject fields , there's also the
> received/created/lastmodified columns, the email size, sender name, and a
> few other fields like the body.

> I thought it'd be relatively simple to find the email.
> Works like a champ on the normalized subject as you noted.
> I can'r find the email if the subject line is null.
> I can't find the email based on any datetime stamp.
> Works like a champ on message size.

> But I figure there can be one or more emails with the same subject line,
> same message size, or same datetimestamp. But all three? I don't think
> so.

> Since the .Find method works fine on the subject and size fields, I'm
> obviously passing the wrong filter for a null (blank) subject and for the
> date.

> I've tried, and failed on
> .Find("IsEmpty([Subject]")
> .Find("IsNull([Subject]")
> .Find("[Subject] = ''")
> and for dates
> 'Set objMail = .Find("[ReceivedTime]= '1/1/2010 3:34:00 AM'")
> and Jan 1, 2010, January 1,2010, no seconds, no AM, just the date, nothing
> works. Single quotes, double quotes, it doesn't seem to matter.

> Based on the above, do you spot anything obvious that is in error? Or
> other tests I could make? Or a link to compare my code to for finding an
> email?

>
 
S

Salad

Dmitry Streblechenko wrote:


> Searching by subject is a bad idea, to put it mildly :) What happens if you
> have multiple messsages with the same subject?
> Use the EntryID property and open the item using using
> Application.Session.GetItemFromID. There is no reason to search.
>


Wouldn't you know, EntryID is not one of the columns in the table tha is

created in Access if one links a folder.

Well, MS gave us/me the partial information, but not all unfortunately.

The table lets one know if there are attachments or not, but not the

attachment names nor as it appears an easy way to find the email. Thus

I'd need a way to find the email if it had attachments.
 
D

Dmitry Streblechenko

If you cannot get entry id as one of the columns, don't use

File/Import/GetExternalData.

Dmitry Streblechenko (MVP)

-

"Salad" <salad@oilandvinegar.com> wrote in message

news:0LKdnbICRb0Emx7WnZ2dnUVZ_qednZ2d@earthlink.com...
> Dmitry Streblechenko wrote:
>
> > Searching by subject is a bad idea, to put it mildly :) What happens if
> > you have multiple messsages with the same subject?
> > Use the EntryID property and open the item using using
> > Application.Session.GetItemFromID. There is no reason to search.
> >

> Wouldn't you know, EntryID is not one of the columns in the table tha is
> created in Access if one links a folder.

> Well, MS gave us/me the partial information, but not all unfortunately.
> The table lets one know if there are attachments or not, but not the
> attachment names nor as it appears an easy way to find the email. Thus
> I'd need a way to find the email if it had attachments.

>
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
C MailItem Find method doesn't work Using Outlook 0
V How to find mailitem in the inspector is a brand new one Outlook VBA and Custom Forms 2
G Event when creating task from mailitem Outlook VBA and Custom Forms 2
A Run-time error '430' on certain emails when trying to set "Outlook.mailitem" as "ActiveExplorer.Selection.Item" Outlook VBA and Custom Forms 2
U Outbox Message Stuck after reading some MailItem Properties with VBA Outlook VBA and Custom Forms 1
oliv- Best practice for catching mailitem.events Outlook VBA and Custom Forms 0
oliv- How to select an mailitem in explorer with "show as conversation" Outlook VBA and Custom Forms 8
JorgeDario How to capture and save the text, when responding a MailItem? Outlook VBA and Custom Forms 3
JorgeDario how to check a MailItem has a digital signature (SMIME) with vba? Outlook VBA and Custom Forms 1
JorgeDario ¿What property of mailitem can be used like primary key? Outlook VBA and Custom Forms 6
S Outlook VBA rule script to process both MailItem and MeetingItem Using Outlook 0
B right click outlook objects in OL2010 acts on current inbox mailitem Using Outlook 6
C MailItem.SaveAs not working Outlook VBA and Custom Forms 10
G RE:The signature is also inserted if you touch the MailItem. Outlook VBA and Custom Forms 1
B Add signature to MailItem Outlook VBA and Custom Forms 3
C How can I create a new MailItem inside a user folder? Outlook VBA and Custom Forms 4
S Create a new Outlook MailItem in an Outlook folder(not a draft) Outlook VBA and Custom Forms 2
A How to get OOM MailItem Raw data Outlook VBA and Custom Forms 2
S Saved Property of MailItem is copied Outlook VBA and Custom Forms 1
N Getting the attachments in MailItem Outlook VBA and Custom Forms 1
T How to get MailItem.Body without security warning in Outlook 2010 Outlook VBA and Custom Forms 2
S ->[O2007] Parsing each line of a MailItem HTMLBody? Outlook VBA and Custom Forms 2
T How to get Inspector or MailItem from wordEditor Outlook VBA and Custom Forms 6
A Select the position of an attached file in a HTML mailitem Outlook VBA and Custom Forms 1
M MailItem object has no property for when a reply was sent Outlook VBA and Custom Forms 3
B Insert information to MailItem Outlook VBA and Custom Forms 1
E Properties added to MailItem in ItemSend event visible to recipien Outlook VBA and Custom Forms 1
V Setting HTMLBody of new mailItem Outlook VBA and Custom Forms 1
M Activate "Add digital signature to this massage" on a MailItem? Outlook VBA and Custom Forms 1
K importing EML in MailItem Outlook VBA and Custom Forms 1
A mailitem Send issue Outlook VBA and Custom Forms 5
M Get email address from MailItem.To? Outlook VBA and Custom Forms 6
S UserProperties of MailItem object. Outlook VBA and Custom Forms 3
R How to capture a Mailitem Event Outlook VBA and Custom Forms 3
S get current position in message body of mailitem Outlook VBA and Custom Forms 8
S How to get RFC822 format message from the MailItem object. Outlook VBA and Custom Forms 4
J Toolbar button to process current mailitem Outlook VBA and Custom Forms 1
D MailItem from an RSS feed Outlook VBA and Custom Forms 2
J Outlook 2007 crashed when pushing send on a displayed mailitem Outlook VBA and Custom Forms 1
N Memory with MailItem.Send Outlook VBA and Custom Forms 1
P Adding a button in a mailitem Outlook VBA and Custom Forms 1
S Reading mailitem after mail is send give runtime error. Outlook VBA and Custom Forms 1
D Max. length of MSO MailItem.EntryID Outlook VBA and Custom Forms 6
R MailItem.Display() error Outlook VBA and Custom Forms 1
R Clone mailitem Outlook VBA and Custom Forms 5
R MailItem Outlook VBA and Custom Forms 2
D Length of the MailItem.EntryID Outlook VBA and Custom Forms 2
M What is the recommendet way to read/write a user defined Field (Named Property) in the MailItem Obje Outlook VBA and Custom Forms 1
M What is the recommendet way to read/write a user defined Field (Named Property) in the MailItem Obje Outlook VBA and Custom Forms 5
J Parent of moved mailItem Outlook VBA and Custom Forms 5

Similar threads

Top