Felix_Dragonhammer
Board Regular
- Joined
- Apr 7, 2015
- Messages
- 117
Hello, I have a code as follows:
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?
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?