Accessing OUTLOOK Unread mails from Word using VBA
'Download all Unread mails from outlook to word and format it into a table
'Make sure Outlook reference added.( tools -> references -> Microsoft outlook)
Sub DownloadMails()
Dim wordApp As Words
Dim myMailFolder As MAPIFolder
'Creating outlook appliction
Set objApp = CreateObject("Outlook.Application")
'Getting outlook inbox folder
Set myMailFolder = objApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
'Total number of mails in the inbox
Count = myMailFolder.Items.Count
Set wodDoc = ActiveDocument
wodDoc.Content = ""
Set myRange = wodDoc.Content
'display all Unreaded mails
i = 0
While i < Count
i = i + 1
With myMailFolder.Items(i)
If (.UnRead) Then
myRange.InsertAfter .Subject & "|" & .ReceivedTime & "|" & .SenderName & "|" & .SenderEmailAddress
wodDoc.Content.InsertParagraphAfter
myRange.InsertAfter "---------------------------------------------------------------------------------------------"
wodDoc.Content.InsertParagraphAfter
myRange.InsertAfter .Body
wodDoc.Content.InsertParagraphAfter
myRange.InsertAfter "---------------------------------------END OF THE MESSAGE------------------------------------"
wodDoc.Content.InsertParagraphAfter
End If
End With
Wend
End Sub
'Make sure Outlook reference added.( tools -> references -> Microsoft outlook)
Sub DownloadMails()
Dim wordApp As Words
Dim myMailFolder As MAPIFolder
'Creating outlook appliction
Set objApp = CreateObject("Outlook.Application")
'Getting outlook inbox folder
Set myMailFolder = objApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
'Total number of mails in the inbox
Count = myMailFolder.Items.Count
Set wodDoc = ActiveDocument
wodDoc.Content = ""
Set myRange = wodDoc.Content
'display all Unreaded mails
i = 0
While i < Count
i = i + 1
With myMailFolder.Items(i)
If (.UnRead) Then
myRange.InsertAfter .Subject & "|" & .ReceivedTime & "|" & .SenderName & "|" & .SenderEmailAddress
wodDoc.Content.InsertParagraphAfter
myRange.InsertAfter "---------------------------------------------------------------------------------------------"
wodDoc.Content.InsertParagraphAfter
myRange.InsertAfter .Body
wodDoc.Content.InsertParagraphAfter
myRange.InsertAfter "---------------------------------------END OF THE MESSAGE------------------------------------"
wodDoc.Content.InsertParagraphAfter
End If
End With
Wend
End Sub


0 Comments:
Post a Comment
<< Home