Hi!
I have a macro that fetches a file from an email in Outlook every day, and then runs a number of other vba codes on it.
The macro runs great, but only gets the file from "today" and if there is multiple emails it only collects the oldest one.
As a workaround, I sometimes have to email myself yesterdays mail (So it will get "Todays date") in order for it to work.
And if i get multiple mails, I have to move them around to folders etc. so the macro will choose the right one.
I suppose making a vba to be able to choose or click on what attachment I would like to use, would be extremely long and complex,
but Is there some way to get it to collect the "newest" one in the inbox from the code provided below?
(I suspect the "%today" is relevant to it?)
Appreciate any help on the subject.
Best reggards:
Wigarth
I have a macro that fetches a file from an email in Outlook every day, and then runs a number of other vba codes on it.
The macro runs great, but only gets the file from "today" and if there is multiple emails it only collects the oldest one.
As a workaround, I sometimes have to email myself yesterdays mail (So it will get "Todays date") in order for it to work.
And if i get multiple mails, I have to move them around to folders etc. so the macro will choose the right one.
I suppose making a vba to be able to choose or click on what attachment I would like to use, would be extremely long and complex,
but Is there some way to get it to collect the "newest" one in the inbox from the code provided below?
(I suspect the "%today" is relevant to it?)
VBA Code:
Sub Get_Report()
Dim colItems As Items, rst As Items, j%, att As Attachment, i%, olapp As Outlook.Application
On Error Resume Next
Set olapp = GetObject(, "Outlook.Application")
Err.Clear: On Error GoTo 0
If olapp Is Nothing Then Set olapp = CreateObject("Outlook.Application")
Set colItems = olapp.Session.GetDefaultFolder(olFolderInbox).Items ' desired folder
Set rst = colItems.Restrict("@SQL=" & "%today(" & AddQ("urn:schemas:httpmail:datereceived") & ")%")
If rst.Count = 0 Then
Exit Sub
Else
End If
For i = 1 To rst.Count
For j = 1 To rst.Item(i).Attachments.Count
Set att = rst.Item(i).Attachments.Item(j)
If att.FileName Like "BaRe Oslo 2*" Then
att.SaveAsFile "L:\kladd.xlsx"
Set att = Nothing: Set rst = Nothing
Set colItems = Nothing: Set olapp = Nothing
Exit Sub
End If
Next j, i
Set att = Nothing: Set rst = Nothing
Set colItems = Nothing: Set olapp = Nothing
'Some more stuff to do after here.
End Sub
Appreciate any help on the subject.
Best reggards:
Wigarth