Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 312
Default saving Outlook attachments

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


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 595
Default saving Outlook attachments

Steph wrote:
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.


--
Dick Kusleika
Excel MVP
Daily Dose of Excel
www.dicks-blog.com


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
cannot open Excel (.xls) attachments in Outlook childofthe1980s Excel Discussion (Misc queries) 1 May 29th 10 12:11 AM
Excel Attachments not opening in Outlook gish Excel Discussion (Misc queries) 1 June 6th 06 09:49 PM
Outlook Webmail and Attachments Jane Excel Discussion (Misc queries) 0 October 12th 05 06:19 PM
Saving attachments from Outlook Jamie Excel Programming 5 May 17th 05 07:08 PM
unblock .exe attachments in outlook Todd Htutenstine Excel Programming 3 February 3rd 04 11:55 PM


All times are GMT +1. The time now is 12:00 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"