MS Excel Macros4 -- Import details of unread Emails from Outlook to Excel

1,032 views
Skip to first unread message

ashish

unread,
Jun 15, 2012, 10:27:38 AM6/15/12
to excelvb...@googlegroups.com
If you want to import the details like Sender Name , Subject , Received Date , Mail Content of all unread emails from outlook to Excel 


Code to Loop through all unread emails in "Inbox" Folder of outlook and import the details to Excel


Sub Scan_my_outlook_inbox()
'TOOLS ->Refrence -> microsoft outlook
'declare outlook objects
Dim olapp As Outlook.Application
Dim olappns As Outlook.Namespace
Dim oinbox As Outlook.Folder
Dim oitem As Outlook.MailItem
Dim myItems As Outlook.Items
Dim i As Long
i = 2
'set outlook objects
Set olapp = New Outlook.Application
Set olappns = olapp.GetNamespace("MAPI")
' it will scan inbox folder only
Set oinbox = olappns.GetDefaultFolder(olFolderInbox)

' check if any unread email in inbox
    If oinbox.Items.Restrict("[UnRead] = True").Count = 0 Then
        MsgBox "NO Unread Email In Inbox"
        Exit Sub
    End If

' sort emails on recieved basis
Set myItems = oinbox.Items
myItems.Sort "[Received]", True

'loop through all unread emails

    For Each oitem In myItems.Restrict("[UnRead] = True")
        Sheets("Inbox Scan").Cells(i, 1).Value = oitem.SenderName
        Sheets("Inbox Scan").Cells(i, 2).Value = oitem.SenderEmailAddress
        Sheets("Inbox Scan").Cells(i, 3).Value = oitem.Subject
        Sheets("Inbox Scan").Cells(i, 4).Value = oitem.Body
        Sheets("Inbox Scan").Cells(i, 5).Value = oitem.ReceivedTime
        i = i + 1
    Next
End Sub


Code to Loop through all unread emails in any folder created by user  of outlook and import the details to Excel . 
For Example i have taken folder "My Gmail" in below code.


Sub Scan_my_outlook_folder()

'TOOLS ->Refrence -> microsoft outlook
'declare outlook objects

Dim olapp As Outlook.Application
Dim olappns As Outlook.Namespace
Dim oinbox As Outlook.Folder
Dim oitem As Outlook.MailItem
Dim myItems As Outlook.Items
Dim i As Long
i = 2
'set outlook objects
Set olapp = New Outlook.Application
Set olappns = olapp.GetNamespace("MAPI")
Set oinbox = olappns.GetDefaultFolder(olFolderInbox)
' folder to scan
Set oinbox = oinbox.Folders("My Gmail")


' check if any unread email in folder name  " my gmail "
    If oinbox.Items.Restrict("[UnRead] = True").Count = 0 Then
        MsgBox "NO Unread Email In Inbox"
        Exit Sub
    End If

' sort emails on recieved basis
Set myItems = oinbox.Items
myItems.Sort "[Received]", True

'loop through all unread emails

    For Each oitem In myItems.Restrict("[UnRead] = True")
        Sheets("Specific Folder").Cells(i, 1).Value = oitem.SenderName
        Sheets("Specific Folder").Cells(i, 2).Value = oitem.SenderEmailAddress
        Sheets("Specific Folder").Cells(i, 3).Value = oitem.Subject
        'Sheets("Specific Folder").Cells(i, 4).Value = oitem.Body
        Sheets("Specific Folder").Cells(i, 5).Value = oitem.ReceivedTime
        i = i + 1
    Next

End Sub

Code to Loop through all unread emails in all folders in outlook and import the details to Excel .

Dim oitem As Outlook.MailItem
Dim i As Long
Sub all_folder_scan()
'Tools Reference Microsoft Outlook
Dim olapp As Outlook.Application
Dim olappns As Outlook.Namespace
Dim oinbox As Outlook.Folder
Dim oFolder As Outlook.MAPIFolder
i = 2
'tools->refrence->microsoft outlook
Set olapp = New Outlook.Application
Set olappns = olapp.GetNamespace("MAPI")
' set inbox folder
Set oinbox = olappns.GetDefaultFolder(olFolderInbox)
    For Each oitem In oinbox.Items.Restrict("[UnRead] = True")
        Sheets("All Folders Scan").Cells(i, 5).Value = oitem.Subject
        Sheets("All Folders Scan").Cells(i, 4).Value = oitem.SenderEmailAddress
        Sheets("All Folders Scan").Cells(i, 3).Value = oitem.SenderName
        Sheets("All Folders Scan").Cells(i, 6).Value = oitem.Body
        Sheets("All Folders Scan").Cells(i, 7).Value = oitem.ReceivedTime
        Sheets("All Folders Scan").Cells(i, 2).Value = oinbox.Name
        Sheets("All Folders Scan").Cells(i, 1).Value = oinbox.FolderPath
        i = i + 1
    Next
    For Each oFolder In oinbox.Folders
        Call subfolders_go(oFolder)
    Next
End Sub
Private Sub subfolders_go(oParent As Outlook.Folder)
Dim oFolder1 As Outlook.MAPIFolder
    For Each oitem In oParent.Items.Restrict("[UnRead] = True")
        Sheets("All Folders Scan").Cells(i, 5).Value = oitem.Subject
        Sheets("All Folders Scan").Cells(i, 4).Value = oitem.SenderEmailAddress
        Sheets("All Folders Scan").Cells(i, 3).Value = oitem.SenderName
        Sheets("All Folders Scan").Cells(i, 6).Value = oitem.Body
        Sheets("All Folders Scan").Cells(i, 7).Value = oitem.ReceivedTime
        Sheets("All Folders Scan").Cells(i, 2).Value = oParent.Name
        Sheets("All Folders Scan").Cells(i, 1).Value = oParent.FolderPath
        i = i + 1
    Next
    If (oParent.Folders.Count > 0) Then
        For Each oFolder1 In oParent.Folders
            Call subfolders_go(oFolder1)
        Next
    End If
End Sub












ashish

unread,
Jun 15, 2012, 11:03:34 AM6/15/12
to excelvb...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages