saving Outlook attachments

  • Thread starter Thread starter Steph
  • Start date Start date
S

Steph

Hello. I have some code below that takes all the emails within an inbox and
saves each .xls attachment to a predefined path on the server. This code
has worked great during internal testing (ie when peoplw within our domain
send emails to the inbox). BUT, when someone from outside the company sends
an e-mail to it, the email comes in as a "discussion" posting to the inbox.
The code below doesn't seem to recognize that and it errors out (Type
mismatch on the line For i = fldr.Items.Count To 1 Step -1). Any ideas?
Thanks!

Sub SaveAtt()
'Saves attachments to a folder

Dim ol As Outlook.Application
Dim ns As Namespace
Dim fldr As MAPIFolder
Dim Mi As MailItem
Dim Att As Attachment
Dim i As Long
Dim iFile As Long
Dim MyPath As String

Set ol = New Outlook.Application
Set ns = ol.GetNamespace("MAPI")
Set fldr = ns.Folders("Public Folders").Folders("All Public
Folders").Folders("My Folder")

MyPath = "\server\myserver\myfolder\"

For i = fldr.Items.Count To 1 Step -1
Set Mi = fldr.Items(i)

If Mi.Attachments.Count > 0 Then
For Each Att In Mi.Attachments
iFile = iFile + 1

Att.SaveAsFile MyPath & Format(Mi.ReceivedTime,
"yyyymmddhhmmss") & "-" & CStr(iFile) & ".xls"

Next Att
Mi.Save

End If
Next i

Set Att = Nothing
Set Mi = Nothing
Set fldr = Nothing
Set ns = Nothing
Set ol = Nothing
Set MoveToFldr = Nothing

End Sub
 
Steph said:
Hello. I have some code below that takes all the emails within an
inbox and saves each .xls attachment to a predefined path on the
server. This code has worked great during internal testing (ie when
peoplw within our domain send emails to the inbox). BUT, when
someone from outside the company sends an e-mail to it, the email
comes in as a "discussion" posting to the inbox. The code below
doesn't seem to recognize that and it errors out (Type mismatch on
the line For i = fldr.Items.Count To 1 Step -1). Any ideas? Thanks!

I'd be surprised if that's the line that gives the error.
[snip]
Dim Mi As MailItem [snip]

For i = fldr.Items.Count To 1 Step -1
Set Mi = fldr.Items(i)

I would guess it's this line. If Items(i) isn't a MailItem, then you will
get a type mismatch here. I don't know what kind of object a "discussion
posting" is, but run the code until you get the error. The, in the
immediate window, type

?Typename(fldr.Items(i))

and see what is says. It may be a matter of dimming Mi as Object so that it
can handle two different kinds of objects. However, both objects would have
to have the same properties (like the Attachments property) or you would
have to test the Typename() and run different code based on the result.

In general, I use

For i = ...
If TypeName(fldr.Items(i)) = "MailItem" Then
Do stuff
End If
Next i

This makes sure that if someone sticks a TaskItem in the Inbox it won't blow
up the code. It sounds like you have a different object that you actually
want to work on in addition to working on MailItem objects.
 
Back
Top