Playing with Journal Items & Outlook Object Model # 3
Reading Journal attachments using Outlook Object Model:
This time, i tried to read all the email and their attachments using Outlook Object Model. I used the following code snippet to do this.
'Code Snippet : How to retrieve Outlook attachments using Visual Basic for Application (VBA)
Dim omailitem As Outlook.JournalItem
Dim myJournal As Outlook.Items
Dim oattach As Outlook.Attachment
Set myNamespace = Application.GetNamespace("MAPI")
Set myJournal = myNamespace.GetDefaultFolder(olFolderJournal).Items
'Total Items available in Folder
Debug.Print "Total Items available in Folder : " & myJournal.Count
For Each Item In myJournal
Set omailitem = Item
'Mail items - Journal Subject & its size
Debug.Print "Item Subject :" & omailitem.Subject & " Size (in bytes):" & omailitem.Size
If omailitem.Attachments.Count > 0 Then
For Each oattach In omailitem.Attachments
If oattach.Type = Outlook.olByReference Then
'If the attachment is ByReference
Debug.Print "By reference : " & oattach.FileName & " Size (in bytes) : " & oattach.Size
ElseIf oattach.Type = Outlook.olByValue Then
'If the attachment is ByValue
Debug.Print "By Value : " & oattach.FileName & " Size (in bytes) : " & oattach.Size
ElseIf oattach.Type = Outlook.olEmbeddeditem Then
'If the attachment is Embdedded
Debug.Print "Embedded item : " & oattach.FileName & " Size (in bytes) : " & oattach.Size
ElseIf oattach.Type = Outlook.olOLE Then
'If the attachment is OLE
Debug.Print "OLE : " & oattach.FileName & " Size (in bytes) :" & oattach.Size
End If
Debug.Print "-------------------"
Next
Oops, i got the values like the following:
Reading attachments and save the attachments in local physical drive/folder:
I done some tweaking with the above code snippet – i am done !! You can see how simple to do programming with Outlook Object Model in few minutes…
Sub JournalMailAttachmentRead()
'Code Snippet : How to retrieve Outlook attachments using Visual Basic for Application (VBA)
Dim omailitem As Outlook.JournalItem
Dim myJournal As Outlook.Items
Dim oattach As Outlook.Attachment
Set myNamespace = Application.GetNamespace("MAPI")
Set myJournal = myNamespace.GetDefaultFolder(olFolderJournal).Items
Debug.Print "Total Items available in Folder : " & myJournal.Count
For Each Item In myJournal
Set omailitem = Item
Debug.Print "Item Subject :" & omailitem.Subject & " Size (in bytes):" & omailitem.Size
If omailitem.Attachments.Count > 0 Then
For Each oattach In omailitem.Attachments
If oattach.Type = Outlook.olByReference Then
Debug.Print "By reference : " & oattach.FileName & " Size (in bytes) : " & oattach.Size
ElseIf oattach.Type = Outlook.olByValue Then
Debug.Print "By Value : " & oattach.FileName & " Size (in bytes) : " & oattach.Size
ElseIf oattach.Type = Outlook.olEmbeddeditem Then
Debug.Print "Embedded item : " & oattach.FileName & " Size (in bytes) : " & oattach.Size
ElseIf oattach.Type = Outlook.olOLE Then
Debug.Print "OLE : " & oattach.FileName & " Size (in bytes) :" & oattach.Size
End If
Debug.Print "-------------------"
Dim filepath As String
filepath = "C:\"
oattach.SaveAsFile filepath & oattach.FileName
Next
End If
Next
End Sub
Happy programming!!