Monday, December 25, 2017

Search mail in outlook with a particular word in the subject and read the message in the mail line by lines using VBA,VBA Teacher Sourav,Kolkata 09748184075


Sub CloseOutlook()
Dim OL As Object
On Error Resume Next
Set OL = GetObject(, "Outlook.Application")
On Error GoTo 0
If OL Is Nothing Then
MsgBox "Outlook is not running!"
Else
OL.Quit
End If
End Sub


 Sub Search_Inbox()
 'Trying to close outlook first

 CloseOutlook


Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.Namespace
Dim myInbox As Outlook.MAPIFolder
Dim myitems As Outlook.Items
Dim myItem As Object
Dim Found As Boolean
Dim atmt As Outlook.Attachment
Dim MyAr() As String
On Error Resume Next

Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderSentMail)
Set myitems = myInbox.Items
Found = False

For Each myItem In myitems
    If myItem.Class = olMail Then
        If InStr(1, myItem.Subject, "macro") > 0 Then
            'MsgBox ("Found")
            For Each atmt In myItem.Attachments

If atmt.FileName = "macro.txt" Then
atmt.SaveAsFile "D:\" & atmt.FileName

MyAr = Split(myItem.Body, vbCrLf)

For i = LBound(MyAr) To UBound(MyAr)
    '~~> This will give you the contents of your email
    '~~> on separate lines
    MsgBox (MyAr(i))
Next i
MsgBox ("Done")

End If



Next
            Found = True
        End If
    End If
Next myItem

'If the subject isn't found:
If Not Found Then
   ' NoResults.Show
End If

myOlApp.Quit
Set myOlApp = Nothing

End Sub

No comments:

Post a Comment