다음을 통해 공유


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:

image

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!!

Comments

  • Anonymous
    October 15, 2009
    Great work DeVa. I appreciate your work for Journal programming series, my dear co-blogger. I am a MVP & Exchange server expert blogger wrote couple of articles. For years i was trying to find the related content on Journal Programming from MSDN, TechNet and prominent 3rd party forums. Honestly I don't find a single one, till the day. Good work & extremely apprecitable - as you provided in-depth documentation, code sample and its output using OOM. Note: Already suggested the same article to be part of MSDN documentation using MS Connect Program to Microsoft - which they missed it earlier.

  • Anonymous
    October 15, 2009
    Great work DeVa. I appreciate your work for Journal programming series, my dear co-blogger. I am a MVP & Exchange server (Exchange 2000, 2003, 2007 onwards) expert blogger wrote couple of articles. For years i was trying to find the related content on Journal Programming from MSDN, TechNet and prominent 3rd party forums. Honestly I don't find a single one, till the day. Good work & extremely apprecitable - as you provided in-depth documentation, code sample and its output using OOM. Note: Already suggested the same article to be part of MSDN documentation using MS Connect Program to Microsoft - which MS missed earlier. (https://connect.microsoft.com/LiveMesh/feedback/ViewFeedback.aspx?FeedbackID=498076#details)