View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Jacob Skaria Jacob Skaria is offline
external usenet poster
 
Posts: 8,520
Default Saving outlook attachments with VBA code

Is this generic ID setup in outlook or are you accessing this generic ID
using OWA.

(ToolsEmail accounts)

If this post helps click Yes
---------------
Jacob Skaria


"ewan7279" wrote:

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

"Jacob Skaria" wrote:

In the code the path mentioned is. Have you changed this and if so check
whether the path is a valid path and ends with "\"

MyPath = "C:\My Documents\Completed Survey\"

If this post helps click Yes
---------------
Jacob Skaria


"ewan7279" wrote:

Hi all,

I have my own outlook inbox and also a generic inbox I share with other
shift members. I have been able to save attachments from emails to my own
inbox using the code found at
http://www.dicks-clicks.com/excel/ol...ve_Attachments , but am
unable to do the same with my generic inbox - I get a message saying the
folder does not exist.

Does anyone know if/how this code can be adjusted to work on a generic
mailbox?

Thanks in advance...