Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Outlook / Programming VBA / April 2007

Tip: Looking for answers? Try searching our database.

Using Restrict method to select emails

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jerry West - 25 Apr 2007 18:15 GMT
I'm trying to use the .Restrict method to select emails within the Inbox:

   sSearch$ = "[From] = 'Jake Parlance'"

   Set fldFolder = CRM.O.AppNameSpace.GetDefaultFolder(olFolderInbox)

   Set objItemsCollection = fldFolder.Items.Restrict(sSearch$)

This works just fine if the email in question has in its From field the name
Jake Parlance. But if I only know Jake Parlance's email address then it
would fail:

sSearch$ = "[From] = 'jparlance@fake.email.com'"

I assume it fails because the email address is "resolved" to the person's
actual name. I'd like to be able to search using only the email address
without needing to know the person's name. Is there a way to do this? I
tried:

sSearch$ = "[E-mail] = 'jparlance@fake.email.com'"

This did not work because I assume "[E-mail]" is not a "keyword"? Is it
possible to search using email only?

Also, where does one find a list of all the "keywords", like "[From]"?

Thanks!

JW
Dmitry Streblechenko - 25 Apr 2007 18:28 GMT
Use the SenderEmailAddress property in your restriction.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy  - Outlook, CDO
and MAPI Developer Tool

> I'm trying to use the .Restrict method to select emails within the Inbox:
>
[quoted text clipped - 25 lines]
>
> JW
Jerry West - 25 Apr 2007 19:01 GMT
Thank you for your answer. Where does one find a list of these "keywords"?
For, example SenderEmailAddress or From? Is there a list of these keywords
anywhere?

Is "To" a valid keyword? I am trying to select emails based on who the email
was sent to from within the Sent Items folder:

   sSearch$ = "[To] = 'jake.fake@bademail.com'"

   Set fldFolder = CRM.O.AppNameSpace.GetDefaultFolder(olFolderSentMail)

   Set objItemsCollection = fldFolder.Items.Restrict(sSearch$)

The objItemsCollection.Count property is always 0. So I'm wondering if "To"
is a keyword? If not, what should I be using and where does one find these
keywords?

Thanks,

JW

> Use the SenderEmailAddress property in your restriction.
>
[quoted text clipped - 32 lines]
>>
>> JW
Dmitry Streblechenko - 25 Apr 2007 19:33 GMT
It is just an Outlook property name, as in MailItem.SenderEmailAddress. Have
you actually tried it?
To property in most cases includes both names and addresses and corresponds
to the PR_DISPLAY_TO MAPI property (look at messages with MFCMAPI or
OutlookSpy), so your restriction won't match any messages.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy  - Outlook, CDO
and MAPI Developer Tool

> Thank you for your answer. Where does one find a list of these "keywords"?
> For, example SenderEmailAddress or From? Is there a list of these keywords
[quoted text clipped - 54 lines]
>>>
>>> JW
Jerry West - 25 Apr 2007 19:42 GMT
>> as in MailItem.SenderEmailAddress. Have you actually tried it?

Yes, it worked great.

>> To property in most cases includes both names and addresses

So then the code I show should work, right? I checked the email and I see
that the "To" field shows the email address versus the actual name:

   sSearch$ = "[To] = 'jake.fake@bademail.com'"

   Set fldFolder = CRM.O.AppNameSpace.GetDefaultFolder(olFolderSentMail)

   Set objItemsCollection = fldFolder.Items.Restrict(sSearch$)

Is the above not valid for selecting an email address of
jake.fake@bademail.com within the Sent Items folder? If not, what is the
proper way to do it? Can you show an example? I have tried the above on my
system and it does not locate the email address in the Sent Items folder
that I know is there.

Thanks,

JW

> It is just an Outlook property name, as in MailItem.SenderEmailAddress.
> Have you actually tried it?
[quoted text clipped - 65 lines]
>>>>
>>>> JW
Dmitry Streblechenko - 25 Apr 2007 19:55 GMT
No, [To] won't work since you are not including the name (it may or rmay not
be there)- look at messages with OutlookSpy. most likely To (and
PR_DISPLAY_TO MAPI) will be something like "Jerry West" or "Jerry West
<jw@comcast.net>"
Do use SenderEmailAddress.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy  - Outlook, CDO
and MAPI Developer Tool

>>> as in MailItem.SenderEmailAddress. Have you actually tried it?
>
[quoted text clipped - 90 lines]
>>>>>
>>>>> JW
Jerry West - 25 Apr 2007 20:41 GMT
I can look at the message in the VB6 IDE. I see that the .To property reads
jake.fake@bademail.com. No name is included.

What I am trying to do is select a email based on the To property (or any
property that indicates who the email was sent to).

Since I have one email in the Sent Items folder and because I can see that
this item's To property value is set at jake.fake@bademail.com I setup my
search string like so:

   sSearch$ = "[To] = 'jake.fake@bademail.com'"

I then search the Sent Items folder for the email:

   Set fldFolder = CRM.O.AppNameSpace.GetDefaultFolder(olFolderSentMail)

   Set objItemsCollection = fldFolder.Items.Restrict(sSearch$)

Unfortunately, this fails. If the To property, as viewed in the debug window
of the VB6 IDE, is set at jake.fake@bademail.com then why would this code
fail?

The SenderEmailAddress property can not be used in this scenario since it
does not return who the email was sent to. The To property and
SenderEmailAddress do not correspond.

JW

> No, [To] won't work since you are not including the name (it may or rmay
> not be there)- look at messages with OutlookSpy. most likely To (and
[quoted text clipped - 103 lines]
>>>>>>
>>>>>> JW
Dmitry Streblechenko - 25 Apr 2007 21:28 GMT
I have no problem running the following script

set Folder = Application.ActiveExplorer.CurrentFolder
set RestrItems = Folder.Items.Restrict("[To] = 'Dmitry Streblechenko'")
MsgBox RestrItems.Count

Can you run it from OutlookSpy? Select a folder where you want to search,
click the "Script Editor" button on the OutlookSpy toolbar, paste the
script, modify the condition appropriately, click Run.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy  - Outlook, CDO
and MAPI Developer Tool

>I can look at the message in the VB6 IDE. I see that the .To property reads
>jake.fake@bademail.com. No name is included.
[quoted text clipped - 132 lines]
>>>>>>>
>>>>>>> JW
Jerry West - 25 Apr 2007 22:10 GMT
> Can you run it from OutlookSpy? Select a folder where you want to search,
> click the "Script Editor" button on the OutlookSpy toolbar, paste the
> script, modify the condition appropriately, click Run.

set Folder = Application.ActiveExplorer.CurrentFolder
set RestrItems = Folder.Items.Restrict("[To] = 'jake.fake@bademail.com'")
MsgBox RestrItems.Count

Not sure how this was going to be different than what I was doing in VB but
I did try it. As in VB, .Count is zero. It fails to return the email which
has in its To property the email address of jake.fake@bademail.com.

If the To property of the email shows as jake.fake@bademail.com and the VB6
IDE also shows that is what the To property holds then shouldn't my code
work?

set Folder = Application.ActiveExplorer.CurrentFolder
set RestrItems = Folder.Items.Restrict("[To] = 'jake.fake@bademail.com'")
MsgBox RestrItems.Count

I have placed 5 emails in my Sent Items folder. I tried searching for each
one of them. I modified my restrict statement to reflect the value of the To
field whether it was an actual email address or the "resolved" name. In no
case did the search return a hit. There must be some explanation for this. I
am using the above code. I can see within the debug window of the VB IDE
that the Sent Items folder has the emails I am searching for in it --yet if
fails to return them. What could I be doing wrong? Is there another property
I could search upon that would yield the same data as the To property?

JW

JW

>I have no problem running the following script
>
[quoted text clipped - 148 lines]
>>>>>>>>
>>>>>>>> JW
Jerry West - 25 Apr 2007 22:20 GMT
Further testing shows that the problem on exists with the Sent Items folder.
I can search using the To and From fields without issue on folders other
than the Sent Items folder. The search always fails when searching this
folder in particular. Is there some reason why this is? Is there some
condition to searching the Sent Items folder I'm not aware of?

Any help appreciated.

JW

>> Can you run it from OutlookSpy? Select a folder where you want to search,
>> click the "Script Editor" button on the OutlookSpy toolbar, paste the
[quoted text clipped - 183 lines]
>>>>>>>>>
>>>>>>>>> JW
Dmitry Streblechenko - 25 Apr 2007 23:54 GMT
"which has in its To property the email address of jake.fake@bademail.com" -
this is not what your code does, your code restricts on the *exact* match
(which is different from "contains").
Are you sure there are no other characters in the To property? Such as
single quotes? Look at a matching message with OutlookSpy (click IMessage
button). What do you see in PR_DISPLAY_TO?

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy  - Outlook, CDO
and MAPI Developer Tool

>> Can you run it from OutlookSpy? Select a folder where you want to search,
>> click the "Script Editor" button on the OutlookSpy toolbar, paste the
[quoted text clipped - 183 lines]
>>>>>>>>>
>>>>>>>>> JW
Jerry West - 26 Apr 2007 00:14 GMT
The PR_DISPLAY_TO field shows: 'jake.fake@bademail.com'

So it shows the single quote at the beginning of the name and at the end of
the name. So is this not an exact match to that?:

set RestrItems = Folder.Items.Restrict("[To] = 'jake.fake@bademail.com'")

I find it worthy to note that this issue on occurs with the Sent Items
folder. I have no trouble on the other folders I tried. There has to be some
significance to this clue.

JW

> "which has in its To property the email address of
> jake.fake@bademail.com" - this is not what your code does, your code
[quoted text clipped - 197 lines]
>>>>>>>>>>
>>>>>>>>>> JW
Dmitry Streblechenko - 26 Apr 2007 00:54 GMT
Yep, single quotes are not any different from any other character; you must
include them in your restriction.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy  - Outlook, CDO
and MAPI Developer Tool

> The PR_DISPLAY_TO field shows: 'jake.fake@bademail.com'
>
[quoted text clipped - 213 lines]
>>>>>>>>>>>
>>>>>>>>>>> JW
Jerry West - 26 Apr 2007 01:16 GMT
I need to add an additional ' character? So you are saying I need to change
the search string to look like this:

set RestrItems = Folder.Items.Restrict("[To] = ''jake.fake@bademail.com''")

When I try that I get an error. Must the ' character be escaped somehow?

JW

> Yep, single quotes are not any different from any other character; you
> must include them in your restriction.
[quoted text clipped - 222 lines]
>>>>>>>>>>>>
>>>>>>>>>>>> JW
Dmitry Streblechenko - 26 Apr 2007 01:33 GMT
Close :-) You need to include ' 3 times...

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy  - Outlook, CDO
and MAPI Developer Tool

>I need to add an additional ' character? So you are saying I need to change
>the search string to look like this:
[quoted text clipped - 233 lines]
>>>>>>>>>>>>>
>>>>>>>>>>>>> JW
Jerry West - 26 Apr 2007 02:12 GMT
I'll be damned, it worked.

Adding the extra ' character was what was missing. So my next question is;
is there another property that contains an unmodified recipient address? I'm
concerned about needing to make two different search calls just to check a
single email address; one with the extra ' characters and one without. This
appears necessary since sometimes the PR_DISPLAY_TO field will show the
extra ' characters and sometimes it isn't there.

JW

> Close :-) You need to include ' 3 times...
>
[quoted text clipped - 241 lines]
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> JW
Dmitry Streblechenko - 26 Apr 2007 17:48 GMT
On the Extended MAPI level, sure, just create a restriction on
PR_MESSAGE_RECIPIENTS with subrestriction on PR_EMAIL_ADDRESS being an exact
match of the specified value.
I don't think you can do that on the OOM level (but I don't deal with
restrictions in OOM that much).

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy  - Outlook, CDO
and MAPI Developer Tool

> I'll be damned, it worked.
>
[quoted text clipped - 252 lines]
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> JW
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.