Run macro after new email

Is it possible to enable automatic macro execution (run macro) every time a new email arrives? Can something like: Private Sub Application_Startup / Triggered() be added to the code?
Or at least add a command for the macro to click the blue button = (run macro) every time a new email arrives or to automatically click it every 3”?
(To automatically repeat the action ‘- run macro – every 3” (3 seconds)?
I thought the whole code should remain only in excel so that it doesn’t have to be entered into outlook?

Here is the code that works but only when the blue button is clicked.

https://raw.githubusercontent.com/santhosh2r2/youtube/7d3490f62aa5e8f4daa90e28bbb09523d0f6bd98/03_Tech_Tips/41_Import_Outlook_Email.vba.txt

'Clear the range contents
Sub Clear_Range()

    Dim lastRow As Integer
    lastRow = Cells(Rows.Count, 1).End(xlUp).Row
    If lastRow > 4 Then
        ActiveSheet.Range("A5:D" & lastRow).ClearContents
    End If

End Sub

'Import E-Mails from Outlook subroutine
Sub Import_Emails()

    'Empty the range
    Clear_Range
    
    'Create an Outlook Application object
    Dim OutlookApp As Outlook.Application
    
    'Create an Namespace object
    Dim OutlookNamespace As Namespace
    
    'Create a Outlook folder object
    Dim Folder As MAPIFolder
    
    'Object to store the retrieved E-Mails
    Dim OutlookItems As Outlook.items
    
    'Temporary object, used for iteration
    Dim OutlookMail As Variant
    
    'Get the folder name from excel sheet
    Dim FolderName As String
    FolderName = ActiveSheet.Range("D1").Value
    
    'Create an instance of Outlook
    Set OutlookApp = New Outlook.Application
    'Set the namespace
    Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
    'Error handling
    On Error GoTo ExitSub
    
    
    'If the checkbox is not checked, then the folder is at the same level as inbox
    If ActiveSheet.OLEObjects("check").Object.Value = False Then
        Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox).Folders("Forex").Folders("Majors").Folders("USDJPY")
    End If
    
    'If the checkbox is active, then it is a sub-folder of inbox
    If ActiveSheet.OLEObjects("check").Object.Value = True Then
        Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox).Folders("Forex").Folders("Majors").Folders("USDJPY")
    End If
    
        
    'Get the folder items and sort according to the recieved time
    Set OutlookItems = Folder.items
    OutlookItems.Sort "ReceivedTime", True
    
    'Results counter starting from Row 5
    Dim i As Integer
    i = 5
    
    'Print the output
    For Each OutlookMail In OutlookItems
        
        If OutlookMail.ReceivedTime >= ActiveSheet.Range("B1").Value Then
            ActiveSheet.Cells(i, 1).Value = OutlookMail.ReceivedTime
            ActiveSheet.Cells(i, 2).Value = OutlookMail.SenderName
            ActiveSheet.Cells(i, 3).Value = OutlookMail.Subject
            ActiveSheet.Cells(i, 4).Value = OutlookMail.Body
            i = i + 1
        End If
        
    Next OutlookMail
    
    'Display the total number of e-mails retrieved
    ActiveSheet.Range("B2").Value = i - 5
    ActiveSheet.Range("B2").Font.Color = vbBlack
    
    'Reset the obejcts
    Set OutlookItems = Nothing
    Set Folder = Nothing
    Set OutlookNamespace = Nothing
    Set OutlookApp = Nothing
    
    Exit Sub

'Error handling function
ExitSub:
ActiveSheet.Range("B2").Value = "Folder name not found"
ActiveSheet.Range("B2").Font.Color = vbRed

    Set OutlookItems = Nothing
    Set Folder = Nothing
    Set OutlookNamespace = Nothing
    Set OutlookApp = Nothing

End Sub

New contributor

Goran Zoric is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật