Copy Flagged Email list from Outlook to Excel with VBA

etgreer

New Member
Joined
Oct 13, 2016
Messages
7
I would like to use my flagged email list to build a to-do list in excel. I need an excel macro that can:
1. Go into outlook (not sure how to tell it to reach from excel to outlook)
2. Find the messages in Outlook that are flagged
3. Copy the subject line, and date that I have set to follow-up, into excel.

I appreciate any help!
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hello

Code:
Sub Flagged()
Dim oout As Object, ns As Namespace, fld As Folder, item As MailItem, r%
Set oout = CreateObject("Outlook.Application")
Set ns = oout.GetNamespace("MAPI")
On Error Resume Next
Set fld = ns.GetDefaultFolder(olFolderInbox)    ' desired folder
If Err.Number <> 0 Then
    Err.Clear
    MsgBox "No such folder."
    Exit Sub
End If
On Error GoTo 0
r = 2
For Each item In fld.Items
    If item.FlagStatus = olFlagMarked Then
        Cells(r, 1) = item.Subject
        Cells(r, 2) = item.ReminderTime
        Cells(r, 3) = item.TaskDueDate
        r = r + 1
    End If
Next
End Sub
 
Upvote 0
Thanks for the response. I'm getting a "User-defined type not defined" error when trying to define NS as Namespace. Do you know what this could be?
 
Upvote 0
Set the following reference at the Visual Basic Editor:

Tools>References>Microsoft Outlook xx.x Object Library.
 
Upvote 0
Making progress! Now I'm getting a Run-time error '13': Type mismatch over "Next", at the very end of the code. I also changed it to "Next item" but I get the same error. Thoughts?

Sub Email()
Dim oout As Object, ns As Namespace, fld As Folder, item As MailItem, r%
Set oout = CreateObject("Outlook.Application")
Set ns = oout.GetNamespace("MAPI")
On Error Resume Next
Set fld = ns.GetDefaultFolder(olFolderInbox) ' desired folder
If Err.Number <> 0 Then
Err.Clear
MsgBox "No such folder."
Exit Sub
End If
On Error GoTo 0
r = 2
For Each item In fld.Items
If item.FlagStatus = olFlagMarked Then
Cells(r, 1) = item.Subject
Cells(r, 2) = item.ReminderTime
Cells(r, 3) = item.TaskDueDate
r = r + 1
End If
Next item
End Sub
 
Upvote 0
New version:

Code:
' Excel module
Sub Email()
Dim oout As Object, ns As Namespace, fld As folder, item As MailItem, r%, fs
fs = 0                                                  ' testing
'fs = olFlagMarked                                      ' use this one
Set oout = CreateObject("Outlook.Application")
Set ns = oout.GetNamespace("MAPI")
On Error Resume Next
Set fld = ns.GetDefaultFolder(olFolderInbox)            ' desired folder
If Err.Number <> 0 Then
    Err.Clear
    MsgBox "No such folder."
    Exit Sub
End If
On Error GoTo 0
r = 2
For Each item In fld.Items
    If TypeName(item) = "MailItem" Then
        If item.FlagStatus = fs Then
            Cells(r, 1) = item.Subject
            Cells(r, 2) = item.ReminderTime
            Cells(r, 3) = item.TaskDueDate
            Cells(r, 4) = item.TaskStartDate
            Cells(r, 5) = item.FlagRequest
            Cells(r, 6) = item.FlagIcon
            r = r + 1
        End If
    End If
Next
End Sub
 
Upvote 0
Hi, Thank you for your supper effort.
Please note this macro giving error while meets calendar or auto response items in inbox. Can you please extend the code by adding exclusion clase for these
 
Upvote 0
Hi

VBA Code:
Sub Email()
Dim oout As Object, ns As Namespace, fld As Folder, item As Object, r%, fs
fs = 0                                                  ' testing
'fs = olFlagMarked                                      ' use this one
Set oout = CreateObject("Outlook.Application")
Set ns = oout.GetNamespace("MAPI")
On Error Resume Next
Set fld = ns.GetDefaultFolder(olFolderDeletedItems) ' desired folder
If Err.Number <> 0 Then
    Err.Clear
    MsgBox "No such folder."
    Exit Sub
End If
On Error GoTo 0
r = 2
MsgBox fld.Items.Count, , "Total items"
For Each item In fld.Items
    If TypeName(item) = "MailItem" Then
        If item.FlagStatus = fs Then
            Cells(r, 1) = item.Subject
            Cells(r, 2) = item.ReminderTime
            Cells(r, 3) = item.TaskDueDate
            Cells(r, 4) = item.TaskStartDate
            Cells(r, 5) = item.FlagRequest
            Cells(r, 6) = item.FlagIcon
            r = r + 1
        End If
    End If
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,939
Messages
6,175,531
Members
452,651
Latest member
wordsearch

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top