Log in

View Full Version : improper counting names in CC



saban
02-14-2008, 05:14 PM
For Each olItem In fldCurrent

'If olItem.CC = "57filter (57filter@gmail.com)@gmail.com (ppoljak@gmail.com)" Then
'i5 = i5 + 1
Select Case olItem.CC
Case "57filter@gmail.com"
i5 = i5 + 1
End Select
' End If
Next


This finds 2 recipients in CC "57filter@gmail.com and drex@gmail.com" so therefore it does not count and i5 is not increased by one (I want it to be increased)

How do I deal with this
Anyone please

Oorang
02-18-2008, 01:35 PM
Yep, that's because the CC property is all of the CC'd addresses :)

Also don't forget that the items collection is generic and contains more object types than just MailItems. And since not all of those types has CC property you will hit an error if you don't either handle the exception or pretest the object. (I opted for the later.)

This should work for you:
Sub Test()
Const lngLwrBnd_c As Long = 0
Const strDlmtr_c As String = ";"
Const strMailItem_c As String = "MailItem"

Dim olItem As Object
Dim fldCurrent As Outlook.MAPIFolder
Dim i5 As Long

Dim strCCs() As String
Dim lngIndx As Long
Set fldCurrent = Outlook.Session.GetDefaultFolder(olFolderInbox)

For Each olItem In fldCurrent.Items
If TypeName(olItem) = strMailItem_c Then
strCCs = Split(LCase$(olItem.CC), strDlmtr_c, Compare:=vbBinaryCompare)
For lngIndx = lngLwrBnd_c To UBound(strCCs)
Select Case strCCs(lngIndx)
Case "57filter@gmail.com"
i5 = i5 + 1
End Select
Next
End If
Next
MssBox CStr(i5)
End Sub


Edit:
A different way that might be slightly faster is this:

Sub Test2()
Const strTargetEmail_c As String = "57filter@gmail.com"
Const strMailItem_c As String = "MailItem"
Dim olItem As Object
Dim fldCurrent As Outlook.MAPIFolder
Dim i5 As Long
Set fldCurrent = Outlook.Session.GetDefaultFolder(olFolderInbox)
For Each olItem In fldCurrent.Items
If TypeName(olItem) = strMailItem_c Then
If InStrB(LCase$(olItem.CC), strTargetEmail_c) Then
i5 = i5 + 1
End If
End If
Next
MsgBox CStr(i5)
End Sub