Public Sub ProcessInbox()
Dim oOutlook As Outlook.Application
Dim oNs As Outlook.NameSpace
Dim Inbox As Outlook.MAPIFolder, Drafts As Outlook.MAPIFolder
Dim oAttachments As Outlook.Attachments
Dim oAttachment As Outlook.Attachment
Dim iMsgCount As Integer
Dim oMessage As Outlook.MailItem, oMessage1 As Outlook.MailItem
Dim iCtr As Long, iAttachCnt As Long
Dim sFileNames As String
Dim aFileNames() As String
'get reference to inbox
Set oOutlook = New Outlook.Application
Set oNs = oOutlook.GetNamespace("MAPI")
Set Inbox = oNs.GetDefaultFolder(olFolderInbox)
Set Drafts = oNs.GetDefaultFolder(olFolderDrafts)
Set oFldr = Inbox
Debug.Print "Total Items: "; Inbox.Items.Count
Debug.Print "Total Unread items = " & Inbox.UnReadItemCount
For Each oMessage In Inbox.Items
With oMessage
If .Subject = "test mail" Then
Debug.Print .To
Debug.Print .CC
Debug.Print .Subject
If .UnRead Then
Debug.Print "Message has not been read"
Else
Debug.Print "Message has been read"
End If
'reference and save all attachments
With oMessage.Attachments
iAttachCnt = .Count
If iAttachCnt = 1 Then
For iCtr = 1 To iAttachCnt
If UCase(Right$(oMessage.Attachments.Item(iCtr).FileName, 3)) = "MSG" Then
.Item(iCtr).SaveAsFile "N:\" & oMessage.Attachments.Item(iCtr).FileName
Set oMessage1 = CreateItemFromTemplate("N:\" & oMessage.Attachments.Item(iCtr).FileName)
Set oMessage1 = oMessage1.Move(Drafts)
Debug.Print .Item(iCtr).DisplayName
End If
Next iCtr
End If
End With
End If
End With
DoEvents
Next oMessage
Set oAttachment = Nothing
Set oAttachments = Nothing
Set oMessage = Nothing
Set oFldr = Nothing
Set oNs = Nothing
Set oOutlook = Nothing
End Sub