+ Reply to Thread
Results 1 to 4 of 4

Run macro after new email

Hybrid View

  1. #1
    Registered User
    Join Date
    12-19-2024
    Location
    serbia
    MS-Off Ver
    365
    Posts
    2

    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.

    '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
    Administrator's note: Please take the time to review our rules. There aren't many, and they are all important. Our guidelines recommend code tags. I have added them for you this time because you are a new member. --6StringJazzer
    Last edited by 6StringJazzer; 12-27-2024 at 11:43 PM. Reason: please use code tags

  2. #2
    Valued Forum Contributor
    Join Date
    09-18-2023
    Location
    Geogia, USA
    MS-Off Ver
    365
    Posts
    331

    Re: Run macro after new email

    You can reenable the ability to run a script in Outlook rules. https://www.extendoffice.com/documen...t-missing.html then assign a macro to run whenever an email is received

    EDIT: as pointed out by 6StringJazzer, it is possible to utilize https://learn.microsoft.com/en-us/of...cation.newmail.

    Also, Excel really isn't the right place for what you want to do. Unless you simply want to click a button and have Excel find all the new emails since the last time the button was clicked.
    Last edited by jdelano; 12-28-2024 at 11:58 AM.

  3. #3
    Registered User
    Join Date
    12-19-2024
    Location
    serbia
    MS-Off Ver
    365
    Posts
    2

    Re: Run macro after new email

    It seems that it has to be from outlook...Thanks

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,223

    Re: Run macro after new email

    You can also set up a listener in Outlook code to respond to a mail received event, no need to edit the Registry for this.
    the whole code should remain only in excel so that it doesn't have to be entered into outlook
    But you can't do it in Excel.

    If you use a timer in Excel there is no easy way to tell which emails are the new ones that have been received since the last check. It is possible, but the whole thing is easier if you drive it from Outlook.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Macro to Email set of cells with identified email list and subject line with outlook
    By aaron061883 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-20-2020, 01:18 PM
  2. [SOLVED] Email Macro paste image in to email and sent to list in designated range Outlook
    By aaron061883 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-12-2020, 10:51 AM
  3. Multiple Issues: Macro not opening new email message or email body not updating
    By ratcat in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-24-2020, 09:21 AM
  4. attach different worksheet and email them tdifferent email address through macro/vba/addin
    By arunverma004 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-03-2014, 08:20 AM
  5. Email Macro - From excel sheet to Outlook email macro
    By juanes in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-30-2013, 01:59 PM
  6. Macro to PDF a sheet in workbook and email (outlook) to an email address in a cell
    By paul_sykes00 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-17-2012, 12:54 AM
  7. Macro doesn't automatically resolve All email address when email is drafted
    By sonny.thind in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-23-2011, 12:58 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1