Hi!
I get a mail everyday with an attachment.
I am trying to get Excel to open Outlok and save the attachment to my desktop
The file have a new name everyday, but it always starts with "transport*****.xlsx" (if that helps)
The mail i recieve can come from several aresses aswell.
It would be nice if the macro didn't save "thousans" of files but just went trough todays files
I get this code to open Outtlook, but i cant figure out what goes wrong after this.
(I get the Message "Finished" when running the macro)
Anyone knows what can be done here?
Code:
Thanks in advance
Wigrth
I get a mail everyday with an attachment.
I am trying to get Excel to open Outlok and save the attachment to my desktop
The file have a new name everyday, but it always starts with "transport*****.xlsx" (if that helps)
The mail i recieve can come from several aresses aswell.
It would be nice if the macro didn't save "thousans" of files but just went trough todays files
I get this code to open Outtlook, but i cant figure out what goes wrong after this.
(I get the Message "Finished" when running the macro)
Anyone knows what can be done here?
Code:
Option Explicit
Sub Test()
Dim olApp As Object 'Outlook.Application
Dim olNS As Object 'Outlook.Namespace
Dim olItems As Object 'Outlook.Items
Dim olItem As Object 'Outlook.MailItem
Dim olAttach As Object 'Outlook.Attachment
Dim Flg As Boolean
Const olFolderInbox As Long = 6
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")
Flg = True
End If
Set olNS = olApp.GetNamespace("MAPI")
Set olItems = olNS.GetDefaultFolder(olFolderInbox).Items
For Each olItem In olItems
If TypeName(olItem) = "MailItem" Then
If Int(olItem.ReceivedTime) = Date Then
Select Case olItem.SenderEmailAddress
'define wich email adresses to look for attachment from
Case "someone@mail.com", "someone2@mail.com"
On Error Resume Next
Set olAttach = olItem.Attachments.Item(1)
Err.Clear: On Error GoTo 0
If Not olAttach Is Nothing Then
'Look for files with the name under
If olAttach.Filename Like "TRANSPORT*.XLSX" Then
' save the file
olAttach.SaveAsFile Environ("H:\DESKTOP") & Application.PathSeparator & olAttach.Filename
GoTo Finish
End If
End If
End Select
End If
End If
Next
Finish:
MsgBox ("Finished!")
If Flg Then olApp.Quit
End Sub
Thanks in advance
Wigrth