none
Outlook macro don't run as excepted RRS feed

  • Question

  • Hi,

    I create a macro that will move to specific folder when I received a email.

    But it don't run as excepted, it only run when I transfer a new email to myself.

    Any help would be appreciated

    Thanks

    Tuesday, October 30, 2018 2:32 PM

All replies

  • Hi pi66666,

    You need to add an event listener to the Inbox.

    This code will add an event listener to the Inbox. You need to add actions you need in the below code:

    Private WithEvents Items As Outlook.Items
                    Private Sub Application_Startup()
                    Dim olApp As Outlook.Application
                    Dim objNS As Outlook.NameSpace
                    Set olApp = Outlook.Application
                    Set objNS = olApp.GetNamespace("MAPI")
                    ' default local Inbox
                    Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
                    End Sub
                    Private Sub Items_ItemAdd(ByVal item As Object)
                    On Error Goto ErrorHandler
                     Dim Msg As Outlook.MailItem
                     If TypeName(item) = "MailItem" Then
                    Set Msg = item
                    ' ******************
                    ' do something here
                    ' ******************
                    End If
                    ProgramExit:
                      Exit Sub
                    ErrorHandler:
                    MsgBox Err.Number & " - " & Err.Description
                    Resume ProgramExit
                    End Sub

    P.S. you need restart your Outlook before run this code.

    Regards,

    Alina

    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.


    Wednesday, October 31, 2018 5:59 AM