Saturday, December 23, 2017

find mail based on words in subject in outlook and save their attachments,VBA Teacher Sourav,Kolkata 09748184075

Sub Search_Inbox()

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
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
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 "F:\" & atmt.Filename
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


Source:Stackoverflow

No comments:

Post a Comment