TypeName Function

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

Guest

Hi, I would like to get some code that shows me how to use the TypeName
function to determine whether the current item I am about to use is in fact a
mailitem and not something else?
 
There's not much to it:

If TypeName(whatever_item_you_want) = "MailItem" Then
' you know it's a mailItem
 
Or you can use the Class property

if whatever_item_you_want.Class = 43 Then

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

There's not much to it:

If TypeName(whatever_item_you_want) = "MailItem" Then
' you know it's a mailItem
 
Thank you Sue and Dmitry for your posts, I presume therefore that I can
declare a generic object variable called objItem as Object and place that
into the function for a true or false answer as in:
If TypeName(objItem) = "MailItem" Then
'it is confirmed that it is a mail item
Else
'I can display an error message to the user
End if
OR
If TypeName(objItem.Class) = 43 Then
'it is confirmed that it is a mail item
Else
'I can display an error message to the user
End if
Much appreciated.
--
Thanks
David G
Albury, Australia


Dmitry Streblechenko said:
Or you can use the Class property

if whatever_item_you_want.Class = 43 Then

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

There's not much to it:

If TypeName(whatever_item_you_want) = "MailItem" Then
' you know it's a mailItem
 
Hi David,
Thank you Sue and Dmitry for your posts, I presume therefore that I can
declare a generic object variable called objItem as Object and place that
into the function for a true or false answer as in:
If TypeName(objItem) = "MailItem" Then
'it is confirmed that it is a mail item
Else
'I can display an error message to the user
End if

The problem might be with common class names which exist in difference
namespaces, like Folder which exists in Outlook, Scripting, CDO and so on.
To be sure just assign an object to a variable of the type you want and do
proper runtime error handling, e.g.

Dim oMI as Outlook.MailItem
On Error Resume Next
Set oMI = objItem
If Not oMI Is Nothing Then
' it is a MailItem
Else
' it is not a MailItem
End If
If TypeName(objItem.Class) = 43 Then
'it is confirmed that it is a mail item
Else
'I can display an error message to the user
End if

No, this goes without TypeName, just objItem.Class = 43. But this will fail
on Objects which do not expose a Class property. So you would net runtime
error handling as well.
TypeName(objItem.Class) might also fail, when property Class does not exist.
If it does exist if will return the type of the property Class. For Outlook
objects it should be Long, the data type used for the olObjectClass enum.
 
I prefer TypeOf:

If TypeOf obj Is Outlook.MailItem Then
...
ElseIf TypeOf obj Is Outlook.ContactItem Then
...
Endif

It's much faster than comparing strings. Referring to Sven, here you can
fully qualify the object type, and it's not dependent on the existence of
the Class property.

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

Am Sat, 21 Jul 2007 08:44:42 -0400 schrieb Sue Mosher [MVP-Outlook]:
 
Back
Top