Monday, September 8, 2008

Working with Attachments in Outlook Email using .net 2.0

Dim olApp As Microsoft.Office.Interop.Outlook.Application = New ApplicationClass()
Dim oNameSpace As Microsoft.Office.Interop.Outlook.NameSpace = olApp.GetNamespace("MAPI")
Dim oOutL As New Outlook.Application
Dim oMail As Outlook.MailItem

Dim objFolder As MAPIFolder = oNameSpace.GetDefaultFolder(OlDefaultFolders.olFolderInbox)
Dim oItems As Items = objFolder.Items

Dim twmp As Integer = 15
Do While (twmp >= 0)
oMail = CType(objFolder.Items((objFolder.Items.Count - twmp)), MailItem)
If (oMail.UnRead = True) Then
If (oMail.Subject = "Document") Then
oMail.Attachments(1).SaveAsFile("E:\VBABonds\kaushik\" & oMail.Attachments(1).FileName)
End If
End If
twmp = (twmp - 1)
Loop

No comments: