Outlook VBA runs script before the email arrives?

Felix_Dragonhammer

Board Regular
Joined
Apr 7, 2015
Messages
117
Hello, I have a code as follows:

Code:
Sub Save_and_Open_Source1(itm As Outlook.MailItem)
    Dim ns As NameSpace
    Dim Inbox As MAPIFolder
    Dim SubFolder As MAPIFolder
    Dim item As Object
    Dim Atmt As Attachment
        Set Atmt = Nothing
    Dim File_Name As String
    Dim i As Integer
    Dim varResponse As VbMsgBoxResult
    Set ns = GetNamespace("MAPI")
    Set Inbox = ns.GetDefaultFolder(olFolderInbox)
    Set SubFolder = Inbox.Folders("IR GPAU Files")
    
    
    For Each Atmt In SubFolder.Items(SubFolder.Items.Count).Attachments
'Check filename of each attachment and save if it has "xls" extension
            If Right(Atmt.FileName, 4) = "xlsx" Then
            'This path must exist! Change folder name as necessary.
    
    Set FileName = Nothing
    
    File_Name = "C:\Users\sberger\Desktop\GPAU\" & Atmt.FileName
    Atmt.SaveAsFile File_Name
    
    Dim xlApp As Object
    Dim sourceWB As Object
    
    Set xlApp = GetObject(, "Excel.Application")
    With xlApp
        .Visible = True
        .EnableEvents = False
    End With
    Set sourceWB = xlApp.Workbooks.Open(File_Name, , False, , , , , , , True)
            End If
        Next Atmt
    
    
        
    Call Check
    
    
    
 
    
End Sub

This code is called by an Outlook Rule when the email arrives. The thing is, it runs the macro on the email that came in before the email it's supposed to be running on, aka I send the email, the code runs on the previous email in the folder, the email I just sent arrives.

Is there a way to resolve this?
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi,

My guess would be something to do with the fact that you are not using the "itm" object anywhere. I think itm is the mail item that caused the script to be run. So if you look at the attachments belonging to that rather than trawl the folders I think it might work.
 
Upvote 0

Forum statistics

Threads
1,225,738
Messages
6,186,734
Members
453,369
Latest member
juliewar

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