473,385 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Save files attached to an email

How do you programatically save files that are attached to an email to a
specified folder? It could be one file or multiple files attached to the email.
Can it be done if Outlook Express is being used or does Outlook have to be used?

Thanks for all help!

Steve

Nov 12 '05 #1
2 4284
Hi,

I don't now about Outlook Express, but to do it in Outlook adapt the
following function:

Public Function SaveAttachedFiles(strSubject as String) As Boolean
Dim olookApp As Outlook.Application
Dim objAttachments As Outlook.Attachments
Dim Attachment As Outlook.Attachment
Dim myNameSpace As NameSpace
Dim myfolder As Outlook.MAPIFolder
Dim myItem As Outlook.MailItem
Dim strFile As String, strSaved As String
SaveAttachedFiles = False
' create the Outlook session.
Set olookApp = CreateObject("Outlook.Application")
Set myNameSpace = olookApp.GetNamespace("MAPI")
'Get the default Inbox
Set myfolder = _
myNameSpace.GetDefaultFolder(olFolderInbox)
For Each myItem In myfolder.Items
If myItem.Subject = strSubject And myItem.UnRead Then
With myItem
Set objAttachments = myItem.Attachments
For Each Attachment In objAttachments
strFile = "C:\Saved Files\" & Attachment.DisplayName
Attachment.SaveAsFile (strFile)
strSaved = "Saved to " & strFile & " on " & Now()
Next
If Len(Trim(strSaved)) > 0 Then
myItem.Body = strSaved & vbCrLf & vbCrLf & myItem.Body
.Save
End If
.UnRead = False
SaveAttachedFiles= True
Exit For
End With

End If
Next
Set olookApp = Nothing

End Function

HTH

--

Cheers
Mark

Free Access/Office Add-Ins at:
http://mphillipson.users.btopenworld.com/

"Steve" <sp**@nospam.com> wrote in message
news:MT*************@newsread2.news.atl.earthlink. net...
How do you programatically save files that are attached to an email to a
specified folder? It could be one file or multiple files attached to the email. Can it be done if Outlook Express is being used or does Outlook have to be used?
Thanks for all help!

Steve

Nov 12 '05 #2

"Steve" <sp**@nospam.com> escreveu na mensagem
news:MT*************@newsread2.news.atl.earthlink. net...
How do you programatically save files that are attached to an email to a
specified folder? It could be one file or multiple files attached to the email. Can it be done if Outlook Express is being used or does Outlook have to be used?
Thanks for all help!

Steve

Hi Steve,

this (uggly, perhaps) code loops thru the OE Inbox folder and, as it
displays the attached filenames, saved in a temp OE folder. These files can
be copied/saved everywhere.

'************ Begin of code
' notice line wrap

Option Compare Database
Option Explicit

'With Acc97, need to call VBAMAP32.DLL instead of calling MAPI32.DLL
'directly.
'
'************************************************* **
' MAPI Message holds information about a message
'************************************************* **
Type MapiMessage
Reserved As Long
Subject As String
NoteText As String
MessageType As String
DateReceived As String
ConversationID As String
Flags As Long
RecipCount As Long
FileCount As Long
End Type

'************************************************
' MAPIRecip holds information about a message
' originator or recipient
'************************************************
Type MapiRecip
Reserved As Long
RecipClass As Long
Name As String
Address As String
EIDSize As Long
EntryID As String
End Type

'************************************************* *****
' MapiFile holds information about file attachments
'************************************************* *****
Type MAPIfile
Reserved As Long
Flags As Long
Position As Long
PathName As String
FileName As String
FileType As String
End Type

'***************************
' FUNCTION Declarations
'***************************
Declare Function MAPILogon Lib "VBAMAP32.DLL" Alias "BMAPILogon"
(ByVal UIParam&, ByVal user$, ByVal Password$, ByVal Flags&, ByVal
Reserved&, Session&) As Long
Declare Function MAPILogoff Lib "VBAMAP32.DLL" Alias "BMAPILogoff"
(ByVal Session&, ByVal UIParam&, ByVal Flags&, ByVal Reserved&) As
Long
Declare Function MAPIFindNext Lib "VBAMAP32.DLL" Alias "BMAPIFindNext"
(ByVal Session&, ByVal UIParam&, ByVal MsgType$, ByVal SeedMsgID$,
ByVal flag&, ByVal Reserved&, MsgID$) As Long
Declare Function MAPIReadMail Lib "VBAMAP32.DLL" Alias "BMAPIReadMail"
(ByVal Session&, ByVal UIParam&, ByVal MsgID$, ByVal Flags&, ByVal
Reserved&, Message As MapiMessage, Originator As MapiRecip, Recips()
As MapiRecip, files() As MAPIfile) As Long

'**************************
' CONSTANT Declarations
'**************************
'
Global Const MAPI_SUCCESS = 0

Global Const MAPI_UNREAD = 1
Global Const MAPI_RECEIPT_REQUESTED = 2
Global Const MAPI_SENT = 4

'***********************
' FLAG Declarations
'***********************
Global Const MAPI_LOGON_UI = &H1
Global Const MAPI_NEW_SESSION = &H2
Global Const MAPI_DIALOG = &H8
Global Const MAPI_UNREAD_ONLY = &H20
Global Const MAPI_ENVELOPE_ONLY = &H40
Global Const MAPI_PEEK = &H80
Global Const MAPI_GUARANTEE_FIFO = &H100
Global Const MAPI_BODY_AS_FILE = &H200
Global Const MAPI_AB_NOMODIFY = &H400
Global Const MAPI_SUPPRESS_ATTACH = &H800
Global Const MAPI_FORCE_DOWNLOAD = &H1000
Public Function get_email()
Dim Session&, rc&
Dim MessageID As String * 512
Dim Msg As MapiMessage
Dim Originator As MapiRecip
Dim aRecips() As MapiRecip
Dim aFiles() As MAPIfile
Dim strFileName As String
Dim Hwnd As Long
Dim i As Integer

DoCmd.Hourglass True
'Hwnd = Forms![principal].Hwnd
rc& = MAPILogon(Hwnd, vbNullString, vbNullString, _
MAPI_FORCE_DOWNLOAD, 0&, Session&)
rc& = MAPILogoff(Session&, Hwnd, 0&, 0&)

If rc& = MAPI_SUCCESS Then
rc& = MAPILogon(0&, vbNullString, vbNullString, 0&, 0&,
Session&)
rc& = MAPIFindNext(Session&, 0&, vbNullString, vbNullString, _
MAPI_GUARANTEE_FIFO, 0&, MessageID)

Do While rc& = MAPI_SUCCESS
rc& = MAPIReadMail(Session&, 0&, MessageID$, _
MAPI_ENVELOPE_ONLY, _
0&, Msg, Originator, aRecips(), aFiles())
If rc& = MAPI_SUCCESS Then

' If Msg.Subject = "Subject i'm waiting for!" Then

rc& = MAPIReadMail(Session&, 0&, MessageID$, 0&, _
0&, Msg, Originator, aRecips(), aFiles())

' this are working since OE 5 through OE 6
Debug.Print CVDate(Msg.DateReceived)

If Msg.FileCount <> 0 Then

Debug.Print UBound(aFiles()) + 1 & " attached files"

' now, the attached files
For i = 0 To Msg.FileCount - 1

strFileName = StrConv(aFiles(i).FileName,
vbUnicode)

'Here the attached files are actually saved in a
temp folder
Debug.Print "Attached file saved: " &
strFileName

Next
End If
' End If
End If

rc& = MAPIFindNext(Session&, 0&, vbNullString, MessageID$, _
0&, 0&, MessageID)
Loop

rc& = MAPILogoff(Session&, 0&, 0&, 0&)
End If
get_email = rc&
DoCmd.Hourglass False
End Function
'************ end of code

Roberto


Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Agenzia Petracca | last post by:
i wanna send html files (even *.doc) in the body of a mail with this code: <? $userfile = "file.html"; $fp = fopen($userfile, "r"); $file = fread($fp, filesize($userfile)); $file =...
0
by: :crp: | last post by:
This perl script goes through a list of files in IFILE and for each file, it sends it on to sendmail. Attachments need special handling and are signified in the email-file by having a line start with...
1
by: Karen Grube | last post by:
Hi! I hate to bother you all with this, but I don't know how best to approach a particular task. Here's the deal: Once a month I personally (that is, in my own personal inbox on my...
4
by: WJA | last post by:
I'm probably missing something here but I can't understand the following. When 2 users try to save a record with the same primary key (a number field), the first record saves as expected, but the...
3
by: Wizard | last post by:
In Access, using VBA, I am sending emails based on query results. These email include relevant information to a scheduled event the recipient is assigned to. This is working great. What I have...
3
by: Brian Farnhill (MCP VB.NET) | last post by:
Hi, I am having some trouble using the MailMessage object to send an email with more than one attachment. I am working on a web based application where a user can submit information, along...
1
by: Li Pang | last post by:
Hi, I receive daily an email with a attached file from my Outlook inbox. This file must be saved into a specific location manually. I made an app to automate this process. My app can read the...
4
by: Mr Gray | last post by:
Hi Guys, My requirement is to scan an FTP directory for the presence of 4 files and if a specific file exists I can begin to GET those files, read the contents and save the contents into an SQL...
1
by: chennaibala | last post by:
can any one send me mutiple image upload program and save the file name with extension in mysql table.we must cheak uploaded file type like bmp or any image file while uploading. i develop...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.