Sorry,
The problem is the inbox I am trying to save the attached files from.
Currently, I am able to save attachments from emails to my personal inbox,
but not from emails to the generic inbox (where the emails I want to save
attachments from actually arrive into). I currently have: Set Fldr =
olNs.GetDefaultFolder(olFolderInbox), but want to know if the default folder
can be set to a different, generic Mailbox inbox...
The code also fails every time it gets to the end of the loop, so I've had
to add a stupid errorhandler to stop the macro crashing each time. I think
it's something to do with the loop count, but don't know what... any help on
this would also be appreciated.
My actual code is:
Private Sub Workbook_Open()
Application.ScreenUpdating = False
Dim olApp As Outlook.Application
Dim olNs As NameSpace
Dim Fldr As MAPIFolder
Dim MoveToFldr As MAPIFolder
Dim olMi As MailItem
Dim olAtt As Attachment
Dim MyPath As String
Dim i As Long
On Error GoTo ERRORHANDLER
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox) 'would like to change
this...
Set MoveToFldr = Fldr.Folders("2. STPM")
MyPath = "c:\WD forecasts\"
For i = Fldr.Items.Count To 1 Step -1
Set olMi = Fldr.Items(i)
If InStr(1, olMi.Subject, "Today's Trades") > 0 Then
For Each olAtt In olMi.Attachments
olAtt.SaveAsFile MyPath & olAtt.Filename
Next olAtt
olMi.Save
olMi.Move MoveToFldr
End If
Next i
Set olAtt = Nothing
Set olMi = Nothing
Set Fldr = Nothing
Set MoveToFldr = Nothing
Set olNs = Nothing
Set olApp = Nothing
closebook = MsgBox("Process Complete. Do you want to close this workbook?",
vbYesNo)
If closebook = vbYes Then
ActiveWorkbook.Close savechanges = False
End If
ERRORHANDLER:
closebook = MsgBox("Process complete (count failed after event). Do you want
to close this workbook?", vbYesNo)
If closebook = vbYes Then
ActiveWorkbook.Close savechanges = False
End If
Exit Sub
End Sub