find function using a categories / variable

N

Newt

I'm trying to use the find method to find messages based on the categories
value of the current item. I'm not sure where I'm going wrong. It seems to
get the correct value (strID) from the current item, but I can't figure out
how to write the find method so it references that value. Any help? Thanks.

Sub findmessage(MyMail As Outlook.MailItem)
Dim strID As String
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim objMail As Outlook.Items

strID = MyMail.Categories
Set objApp = New Outlook.Application
Set objNS = Application.GetNamespace("MAPI")
Set objFolder = objNS.GetDefaultFolder(olFolderInbox)
Set objMail = objFolder.Items.find("[Categories] = """ & strID & """")
If objMail Is Nothing Then
MsgBox "nothing"
Else
objMail.Display
End If
End Sub
 
K

Ken Slovak - [MVP - Outlook]

If you look at the Object Browser help for Items.Restrict you will see that
you can't use Categories for a Restrict or Find operation. That's because
it's really a multi-valued string field (array).

You might want to use a field like Mileage or other unused field to store
your filter string.
 
N

Newt

I swapped it out to use mileage, but now I get a run-time/type mismatch error.

Sub findmessage(MyMail As Outlook.MailItem)
Dim strID As String
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim objMail As Outlook.Items

strID = MyMail.Mileage
Set objApp = New Outlook.Application
Set objNS = Application.GetNamespace("MAPI")
Set objFolder = objNS.GetDefaultFolder(olFolderInbox)
Set objMail = objFolder.Items.find("[Categories] = """ & strID & """")
If objMail Is Nothing Then
MsgBox "nothing"
Else
objMail.Display
End If
End Sub



Ken Slovak - said:
If you look at the Object Browser help for Items.Restrict you will see that
you can't use Categories for a Restrict or Find operation. That's because
it's really a multi-valued string field (array).

You might want to use a field like Mileage or other unused field to store
your filter string.




Newt said:
I'm trying to use the find method to find messages based on the categories
value of the current item. I'm not sure where I'm going wrong. It seems to
get the correct value (strID) from the current item, but I can't figure
out
how to write the find method so it references that value. Any help?
Thanks.

Sub findmessage(MyMail As Outlook.MailItem)
Dim strID As String
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim objMail As Outlook.Items

strID = MyMail.Categories
Set objApp = New Outlook.Application
Set objNS = Application.GetNamespace("MAPI")
Set objFolder = objNS.GetDefaultFolder(olFolderInbox)
Set objMail = objFolder.Items.find("[Categories] = """ & strID & """")
If objMail Is Nothing Then
MsgBox "nothing"
Else
objMail.Display
End If
End Sub
 
M

Michael Bauer [MVP - Outlook]

You're still using the Categories field in the Find function; as Ken told
you that doesn't work. Instead you would have to store your data in the
Mileage field and compare that.

If you have to fnid items with the same Category as MyMail then you must
loop through the folder's Items collection and compare each item:

Dim obj as Object
Dim i&
Dim Items as Outlook.Items

Set Items=objFolder.Items
For i=1 to Items.Count
Set obj=Items(i)
If obj.Categories = strID Then
' match
Endif
Next

If your approach had worked it had shown only the first match - if any. You
might edit my sample to rebuild that logic.

--
Best regards
Michael Bauer - MVP Outlook
Organize E-Mails:
<http://www.vboffice.net/product.html?id=2006063&cmd=detail&lang=en&pub=6>

Am Fri, 21 Dec 2007 15:19:00 -0800 schrieb Newt:
I swapped it out to use mileage, but now I get a run-time/type mismatch error.

Sub findmessage(MyMail As Outlook.MailItem)
Dim strID As String
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim objMail As Outlook.Items

strID = MyMail.Mileage
Set objApp = New Outlook.Application
Set objNS = Application.GetNamespace("MAPI")
Set objFolder = objNS.GetDefaultFolder(olFolderInbox)
Set objMail = objFolder.Items.find("[Categories] = """ & strID & """")
If objMail Is Nothing Then
MsgBox "nothing"
Else
objMail.Display
End If
End Sub



Ken Slovak - said:
If you look at the Object Browser help for Items.Restrict you will see that
you can't use Categories for a Restrict or Find operation. That's because
it's really a multi-valued string field (array).

You might want to use a field like Mileage or other unused field to store
your filter string.




Newt said:
I'm trying to use the find method to find messages based on the categories
value of the current item. I'm not sure where I'm going wrong. It seems to
get the correct value (strID) from the current item, but I can't figure
out
how to write the find method so it references that value. Any help?
Thanks.

Sub findmessage(MyMail As Outlook.MailItem)
Dim strID As String
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim objMail As Outlook.Items

strID = MyMail.Categories
Set objApp = New Outlook.Application
Set objNS = Application.GetNamespace("MAPI")
Set objFolder = objNS.GetDefaultFolder(olFolderInbox)
Set objMail = objFolder.Items.find("[Categories] = """ & strID & """")
If objMail Is Nothing Then
MsgBox "nothing"
Else
objMail.Display
End If
End Sub
 
N

Newt

Oops. I meant to swap out the "Categories" for "Mileage". It still gives me a
runtime error:

Sub findmessage(MyMail As Outlook.MailItem)


Dim strID As String
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim objMail As Outlook.Items

strID = MyMail.Mileage
Set objApp = New Outlook.Application
Set objNS = Application.GetNamespace("MAPI")
Set objFolder = objNS.GetDefaultFolder(olFolderInbox)
Set objMail = objFolder.Items.find("[Mileage] = """ & strID & """")
If objMail Is Nothing Then
MsgBox "nothing"
Else
objMail.Display
End If
End Sub

Michael Bauer said:
You're still using the Categories field in the Find function; as Ken told
you that doesn't work. Instead you would have to store your data in the
Mileage field and compare that.

If you have to fnid items with the same Category as MyMail then you must
loop through the folder's Items collection and compare each item:

Dim obj as Object
Dim i&
Dim Items as Outlook.Items

Set Items=objFolder.Items
For i=1 to Items.Count
Set obj=Items(i)
If obj.Categories = strID Then
' match
Endif
Next

If your approach had worked it had shown only the first match - if any. You
might edit my sample to rebuild that logic.

--
Best regards
Michael Bauer - MVP Outlook
Organize E-Mails:
<http://www.vboffice.net/product.html?id=2006063&cmd=detail&lang=en&pub=6>

Am Fri, 21 Dec 2007 15:19:00 -0800 schrieb Newt:
I swapped it out to use mileage, but now I get a run-time/type mismatch error.

Sub findmessage(MyMail As Outlook.MailItem)
Dim strID As String
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim objMail As Outlook.Items

strID = MyMail.Mileage
Set objApp = New Outlook.Application
Set objNS = Application.GetNamespace("MAPI")
Set objFolder = objNS.GetDefaultFolder(olFolderInbox)
Set objMail = objFolder.Items.find("[Categories] = """ & strID & """")
If objMail Is Nothing Then
MsgBox "nothing"
Else
objMail.Display
End If
End Sub



Ken Slovak - said:
If you look at the Object Browser help for Items.Restrict you will see that
you can't use Categories for a Restrict or Find operation. That's because
it's really a multi-valued string field (array).

You might want to use a field like Mileage or other unused field to store
your filter string.




I'm trying to use the find method to find messages based on the categories
value of the current item. I'm not sure where I'm going wrong. It seems to
get the correct value (strID) from the current item, but I can't figure
out
how to write the find method so it references that value. Any help?
Thanks.

Sub findmessage(MyMail As Outlook.MailItem)
Dim strID As String
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim objMail As Outlook.Items

strID = MyMail.Categories
Set objApp = New Outlook.Application
Set objNS = Application.GetNamespace("MAPI")
Set objFolder = objNS.GetDefaultFolder(olFolderInbox)
Set objMail = objFolder.Items.find("[Categories] = """ & strID & """")
If objMail Is Nothing Then
MsgBox "nothing"
Else
objMail.Display
End If
End Sub
 
M

Michael Bauer [MVP - Outlook]

You should declare objMail as Object, or if you're sure there're only
MailItems then you might also declare it as MailItem.

--
Best regards
Michael Bauer - MVP Outlook
Synchronize Outlook Categories:
<http://www.vboffice.net/product.html?id=2006063&cmd=detail&lang=en&pub=6>

Am Wed, 26 Dec 2007 14:01:01 -0800 schrieb Newt:
Oops. I meant to swap out the "Categories" for "Mileage". It still gives me a
runtime error:

Sub findmessage(MyMail As Outlook.MailItem)


Dim strID As String
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim objMail As Outlook.Items

strID = MyMail.Mileage
Set objApp = New Outlook.Application
Set objNS = Application.GetNamespace("MAPI")
Set objFolder = objNS.GetDefaultFolder(olFolderInbox)
Set objMail = objFolder.Items.find("[Mileage] = """ & strID & """")
If objMail Is Nothing Then
MsgBox "nothing"
Else
objMail.Display
End If
End Sub

Michael Bauer said:
You're still using the Categories field in the Find function; as Ken told
you that doesn't work. Instead you would have to store your data in the
Mileage field and compare that.

If you have to fnid items with the same Category as MyMail then you must
loop through the folder's Items collection and compare each item:

Dim obj as Object
Dim i&
Dim Items as Outlook.Items

Set Items=objFolder.Items
For i=1 to Items.Count
Set obj=Items(i)
If obj.Categories = strID Then
' match
Endif
Next

If your approach had worked it had shown only the first match - if any. You
might edit my sample to rebuild that logic.
Am Fri, 21 Dec 2007 15:19:00 -0800 schrieb Newt:
I swapped it out to use mileage, but now I get a run-time/type mismatch error.

Sub findmessage(MyMail As Outlook.MailItem)
Dim strID As String
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim objMail As Outlook.Items

strID = MyMail.Mileage
Set objApp = New Outlook.Application
Set objNS = Application.GetNamespace("MAPI")
Set objFolder = objNS.GetDefaultFolder(olFolderInbox)
Set objMail = objFolder.Items.find("[Categories] = """ & strID & """")
If objMail Is Nothing Then
MsgBox "nothing"
Else
objMail.Display
End If
End Sub



:

If you look at the Object Browser help for Items.Restrict you will see that
you can't use Categories for a Restrict or Find operation. That's because
it's really a multi-valued string field (array).

You might want to use a field like Mileage or other unused field to store
your filter string.




I'm trying to use the find method to find messages based on the categories
value of the current item. I'm not sure where I'm going wrong. It
seems
to
get the correct value (strID) from the current item, but I can't figure
out
how to write the find method so it references that value. Any help?
Thanks.

Sub findmessage(MyMail As Outlook.MailItem)
Dim strID As String
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim objMail As Outlook.Items

strID = MyMail.Categories
Set objApp = New Outlook.Application
Set objNS = Application.GetNamespace("MAPI")
Set objFolder = objNS.GetDefaultFolder(olFolderInbox)
Set objMail = objFolder.Items.find("[Categories] = """ & strID & """")
If objMail Is Nothing Then
MsgBox "nothing"
Else
objMail.Display
End If
End Sub
 
N

Newt

Thanks. Your answers helped a lot. Here's the final code I ended up using:

Sub findmessage2(MyTask As Outlook.TaskItem)

Dim strID As String
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim objMail As Outlook.Items
Dim obj As Object
Dim i&

strID = MyTask.Mileage
If strID = "" Then
MsgBox "No message is associated with this task."
Else
Set objNS = Application.GetNamespace("MAPI")
Set objFolder =
objNS.GetFolderFromID("00000000939F98511D97D511806D00805F315D6F0100F7235B72B195E042AA1F428BCA4E35AB000018E900090000")
Set objMail = objFolder.Items
For i = 1 To objMail.Count
Set obj = objMail(i)
If obj.Mileage = strID Then
obj.Display
End If
Next
End If

End Sub

Michael Bauer said:
You should declare objMail as Object, or if you're sure there're only
MailItems then you might also declare it as MailItem.

--
Best regards
Michael Bauer - MVP Outlook
Synchronize Outlook Categories:
<http://www.vboffice.net/product.html?id=2006063&cmd=detail&lang=en&pub=6>

Am Wed, 26 Dec 2007 14:01:01 -0800 schrieb Newt:
Oops. I meant to swap out the "Categories" for "Mileage". It still gives me a
runtime error:

Sub findmessage(MyMail As Outlook.MailItem)


Dim strID As String
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim objMail As Outlook.Items

strID = MyMail.Mileage
Set objApp = New Outlook.Application
Set objNS = Application.GetNamespace("MAPI")
Set objFolder = objNS.GetDefaultFolder(olFolderInbox)
Set objMail = objFolder.Items.find("[Mileage] = """ & strID & """")
If objMail Is Nothing Then
MsgBox "nothing"
Else
objMail.Display
End If
End Sub

Michael Bauer said:
You're still using the Categories field in the Find function; as Ken told
you that doesn't work. Instead you would have to store your data in the
Mileage field and compare that.

If you have to fnid items with the same Category as MyMail then you must
loop through the folder's Items collection and compare each item:

Dim obj as Object
Dim i&
Dim Items as Outlook.Items

Set Items=objFolder.Items
For i=1 to Items.Count
Set obj=Items(i)
If obj.Categories = strID Then
' match
Endif
Next

If your approach had worked it had shown only the first match - if any. You
might edit my sample to rebuild that logic.
Am Fri, 21 Dec 2007 15:19:00 -0800 schrieb Newt:

I swapped it out to use mileage, but now I get a run-time/type mismatch
error.

Sub findmessage(MyMail As Outlook.MailItem)
Dim strID As String
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim objMail As Outlook.Items

strID = MyMail.Mileage
Set objApp = New Outlook.Application
Set objNS = Application.GetNamespace("MAPI")
Set objFolder = objNS.GetDefaultFolder(olFolderInbox)
Set objMail = objFolder.Items.find("[Categories] = """ & strID & """")
If objMail Is Nothing Then
MsgBox "nothing"
Else
objMail.Display
End If
End Sub



:

If you look at the Object Browser help for Items.Restrict you will see
that
you can't use Categories for a Restrict or Find operation. That's because
it's really a multi-valued string field (array).

You might want to use a field like Mileage or other unused field to store
your filter string.




I'm trying to use the find method to find messages based on the
categories
value of the current item. I'm not sure where I'm going wrong. It seems
to
get the correct value (strID) from the current item, but I can't figure
out
how to write the find method so it references that value. Any help?
Thanks.

Sub findmessage(MyMail As Outlook.MailItem)
Dim strID As String
Dim objNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim objMail As Outlook.Items

strID = MyMail.Categories
Set objApp = New Outlook.Application
Set objNS = Application.GetNamespace("MAPI")
Set objFolder = objNS.GetDefaultFolder(olFolderInbox)
Set objMail = objFolder.Items.find("[Categories] = """ & strID & """")
If objMail Is Nothing Then
MsgBox "nothing"
Else
objMail.Display
End If
End Sub
 

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