Forum Discussion
Run macro after new email
To automatically run the macro when a new email arrives without manually clicking a button, you'll need to modify your VBA code. Since you prefer keeping everything in Excel and not modifying Outlook, here are two possible approache proposals:
Approach 1: Run the Macro Every 3 Seconds (Using a Timer)
This is the simplest method, as it avoids using Outlook events and relies solely on Excel. The macro will trigger every 3 seconds, checking for new emails.
Add This Timer Code to Excel VBA
Dim NextRun As Date
' This starts the automatic import every 3 seconds
Sub Start_Auto_Import()
Import_Emails
NextRun = Now + TimeValue("00:00:03") ' Runs every 3 seconds
Application.OnTime NextRun, "Start_Auto_Import"
End Sub
' Stop the automatic import
Sub Stop_Auto_Import()
On Error Resume Next
Application.OnTime NextRun, "Start_Auto_Import", , False
End SubApproach 2: Auto-Trigger Based on Workbook Events
If you want the macro to start automatically when Excel opens, use the Workbook_Open event.
Add This Code to ThisWorkbook Module
Private Sub Workbook_Open()
Start_Auto_Import ' Starts automation when the workbook opens
End SubConsiderations:
- High CPU Usage: Running every 3 seconds might affect Excel’s performance. You can adjust the timer to 10–30 seconds if needed.
- Duplicate Emails: Add checks to prevent importing the same emails repeatedly.
- Error Handling: Ensure proper error handling, especially when Outlook is closed.
My answers are voluntary and without guarantee!
Hope this will help you.
Was the answer useful? Mark as best response and like it!
This will help all forum participants.