Raj
Raj

Reputation: 623

Save attachments when received. "runtime error:13 Type mismatch"

I have several unseen email attachment in email. I would like to download it automatically whenever I receive it from specific sender. I'm using Outlook 2013.

I get:

runtime error:13 Type mismatch

Private Sub Application_NewMail()
    
    Dim onamespace As Outlook.NameSpace
    Set onamespace = Outlook.GetNamespace("MAPI")
    
    Dim myfol As Outlook.Folder
    Set myfol = onamespace.GetDefaultFolder(olFolderInbox)
    
    Dim omail As Outlook.MailItem
    Set omail = Outlook.CreateItem(olMailItem)
    Dim atmt As Outlook.attachment
    
    For Each omail In myfol.Items
    
        If omail.SenderEmailAddress = "@gmail.com" Then
        
            For Each atmt In omail.Attachments
                
                atmt.SaveAsFile "C:\Users\raj\Downloads\" & atmt.fileName
                
            Next
        
        Else
        End If
    
    Next
                
End Sub

Upvotes: 2

Views: 3883

Answers (1)

Alina Li
Alina Li

Reputation: 884

You could create a macro rules, when you receive an email from specific sender run a script.

About automatically save attachment, you could refer to this link:

Public Sub Save_Attachment(olItem As Outlook.MailItem)
    Dim olAttch As Outlook.Attachment
    Dim sPath As String

    'sPath = Environ("USERPROFILE") & "\Documents\"
    sPath = "C:\Temp\"

For Each olAttch In olItem.Attachments
       If olAttch.UnRead = True Then
           If olAttch.SenderEmailAddress = "[email protected]" Then
               olAttch.SaveAsFile sPath & "\" & olAttch.DisplayName
               olAttch.UnRead = false
           End If
       End If
    Next

    Set olAttch = Nothing
End Sub

Reference from:

Save attachment with incoming email sender's name or email address

Upvotes: 1

Related Questions