Search code examples
excelvbaoutlook

How to get the "CC" email addresses in VBA from a specific email folder in Outlook?


How do I retrieve CC email addresses from a mail in an Outlook folder using VBA?

I have this error

Run-time error '91':
Object variable or With block variable not set

Sub CC_EMAIL()
Dim lngCounter As Long
lngCounter = 2
Const PR_EMAIL = &H39FE001E
ThisWorkbook.Sheets(1).Cells(1, 1).Value = "CC Name"
ThisWorkbook.Sheets(1).Cells(1, 2).Value = "CC Email"
'ThisWorkbook.Sheets(1).Cells(1, 3).Value = "Cc-Recipients"
Set objOL = CreateObject("Outlook.Application")
Set objMsg = objOL.ActiveInspector.CurrentItem
Set objSmail = CreateObject("Redemption.SafeMailItem")
objSmail.Item = objMsg
For Each recip In objSmail.Recipients
    If InStr(objSmail.CC, recip.Name) Then
    ThisWorkbook.Sheets(1).Cells(lngCounter, 1).Value = recip.Name
    ThisWorkbook.Sheets(1).Cells(lngCounter, 2).Value = recip.Fields(PR_EMAIL)
    'ThisWorkbook.Sheets(1).Cells(lngCounter, 3).Value = objSmail.CC
    lngCounter = lngCounter + 1
    End If
Next
End Sub

Solution

  • Firstly, check that objOL.ActiveInspector is not null. It will exits only if there an email shown in a separate inspector.

    Secondly, to test whether a recipient is CC, use the Type property. Change the line

    If InStr(objSmail.CC, recip.Name) Then
    

    to

    If recip.Type = 2 Then '2 is olCC