Email Check

vmjan02

Well-known Member
Joined
Aug 15, 2012
Messages
1,132
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
I have this and is working all perfect, need to change 2 this.

1. should also check for subject as well with email ID (currently is only id)
2. after download should close the file and reopen the file after 1 hr 3 min Idea is the code to download the attachment ever hour so delay of 1 hr 3 min

VBA Code:
Sub DownloadAttachment()
    
    Dim olApp As Object
    Dim olNamespace As Object
    Dim olFolder As Object
    Dim olItem As Object
    Dim olAttachment As Object
    Dim savePath As String

    ' Set the path where you want to save the attachment
    savePath = ActiveWorkbook.Path & "\" & "Hourly mail files\"

    ' Check if the specified folder exists, and create it if not
    If Dir(savePath, vbDirectory) = "" Then
        MkDir savePath
    End If

    ' Initialize Outlook
    Set olApp = CreateObject("Outlook.Application")
    Set olNamespace = olApp.GetNamespace("MAPI")

    ' Set the folder to the Inbox (you can change it to the appropriate folder)
    Set olFolder = olNamespace.GetDefaultFolder(6) ' 6 represents the Inbox

    ' Loop through the items in the folder
    For Each olItem In olFolder.Items
        ' Check if the email is from the specified sender
        If olItem.SenderEmailAddress = "xxx@xx" Then
            ' Loop through the attachments in the email
            For Each olAttachment In olItem.Attachments
                ' Check if the attachment is an Excel file
                If Right(olAttachment.fileName, 4) = ".xls" Or Right(olAttachment.fileName, 5) = ".xlsx" Then
                    ' Save the attachment to the specified path
                    olAttachment.SaveAsFile savePath & olAttachment.fileName
                End If
            Next olAttachment
        End If
    Next olItem

    ' Clean up
    Set olAttachment = Nothing
    Set olItem = Nothing
    Set olFolder = Nothing
    Set olNamespace = Nothing
    Set olApp = Nothing

End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Forum statistics

Threads
1,223,884
Messages
6,175,177
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top