Using Fing in UserProperties

G

George Hester

Here is a smippet of the code;

If obj.Class = olMail Then
MsgBox "Here"
Set myMail = obj
MsgBox myMail.Subject
Set usrProperty = myMail.UserProperties
MsgBox usrProperty.Count
Set oSpam = usrProperty.Find("MySpam")
'MsgBox IsError(oSpam.Name)
MsgBox IsObject(oSpam)
End If

This obj lies in a folder for which the MySpam field "exists in the folder."
What exactly that means I do not know but let's assume it does because I am
not getting the error "Field Unknown."

But in the statement Set oSpam = usrProperty.Find("MySpam") I am not getting
an error even when usrProperty.Count = 0. But the error comes in the
following statement MsgBox IsError(oSpam.Name) which is Object variable or
With block variable not set and Error number is H5B.

This presents a problem. What I need is some way to capture the situation
where "MySpam" has not been added to the UserProperties collection for a
specific MailItem and the way I tried it was to raise an error with
oSpam.Name but seems not to be capturable w/o a On Error Resume Next which I
do not want to use.. The next statement resolves to True. Any ideas?
 
D

Dmitry Streblechenko

You need to check whether you get a valid object:

Set oSpam = usrProperty.Find("MySpam")
if Not oSpam IsNothing Then
...

A property added to the folder simply makes it available in the field
chooser when you design a view. A message from that folder is not quaranteed
to have that (or any other) property.

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

George Hester

I like yours better than what I saw at Microsoft's site. They used

If TypeName(oSpam) <> "Nothing" Then

whereas you used

If Not oSpam Is Nothing Then

Thanks man.

When you say, "A property added to the folder simply makes it available in
the field chooser when you design a view." Does that mean I have to add
that field by using the GUI or can I add the field programmatically? I
thought that was what I was doing with the third argument in the
MailItem.UserProperties.Add("MySpam", clYesNo, True, 1) but seems no.
 
D

Dmitry Streblechenko

MailItem.UserProperties.Add is what you need to add a property to that
*particular* message. Other messages in the folder (or anywhere else) will
not be affected.
Don't forget to set the value and save the message:

set Prop = MailItem.UserProperties.Add("MySpam", clYesNo, True, 1)
Prop.Value = TRUE
MailItem.Save

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top