473,545 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4297
Hi,

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

Public Function SaveAttachedFil es(strSubject as String) As Boolean
Dim olookApp As Outlook.Applica tion
Dim objAttachments As Outlook.Attachm ents
Dim Attachment As Outlook.Attachm ent
Dim myNameSpace As NameSpace
Dim myfolder As Outlook.MAPIFol der
Dim myItem As Outlook.MailIte m
Dim strFile As String, strSaved As String
SaveAttachedFil es = False
' create the Outlook session.
Set olookApp = CreateObject("O utlook.Applicat ion")
Set myNameSpace = olookApp.GetNam espace("MAPI")
'Get the default Inbox
Set myfolder = _
myNameSpace.Get DefaultFolder(o lFolderInbox)
For Each myItem In myfolder.Items
If myItem.Subject = strSubject And myItem.UnRead Then
With myItem
Set objAttachments = myItem.Attachme nts
For Each Attachment In objAttachments
strFile = "C:\Saved Files\" & Attachment.Disp layName
Attachment.Save AsFile (strFile)
strSaved = "Saved to " & strFile & " on " & Now()
Next
If Len(Trim(strSav ed)) > 0 Then
myItem.Body = strSaved & vbCrLf & vbCrLf & myItem.Body
.Save
End If
.UnRead = False
SaveAttachedFil es= 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.co m> wrote in message
news:MT******** *****@newsread2 .news.atl.earth link.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.co m> escreveu na mensagem
news:MT******** *****@newsread2 .news.atl.earth link.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.D LL" Alias "BMAPILogon "
(ByVal UIParam&, ByVal user$, ByVal Password$, ByVal Flags&, ByVal
Reserved&, Session&) As Long
Declare Function MAPILogoff Lib "VBAMAP32.D LL" Alias "BMAPILogof f"
(ByVal Session&, ByVal UIParam&, ByVal Flags&, ByVal Reserved&) As
Long
Declare Function MAPIFindNext Lib "VBAMAP32.D LL" Alias "BMAPIFindN ext"
(ByVal Session&, ByVal UIParam&, ByVal MsgType$, ByVal SeedMsgID$,
ByVal flag&, ByVal Reserved&, MsgID$) As Long
Declare Function MAPIReadMail Lib "VBAMAP32.D LL" Alias "BMAPIReadM ail"
(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_RE QUESTED = 2
Global Const MAPI_SENT = 4

'************** *********
' FLAG Declarations
'************** *********
Global Const MAPI_LOGON_UI = &H1
Global Const MAPI_NEW_SESSIO N = &H2
Global Const MAPI_DIALOG = &H8
Global Const MAPI_UNREAD_ONL Y = &H20
Global Const MAPI_ENVELOPE_O NLY = &H40
Global Const MAPI_PEEK = &H80
Global Const MAPI_GUARANTEE_ FIFO = &H100
Global Const MAPI_BODY_AS_FI LE = &H200
Global Const MAPI_AB_NOMODIF Y = &H400
Global Const MAPI_SUPPRESS_A TTACH = &H800
Global Const MAPI_FORCE_DOWN LOAD = &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_DOWN LOAD, 0&, Session&)
rc& = MAPILogoff(Sess ion&, Hwnd, 0&, 0&)

If rc& = MAPI_SUCCESS Then
rc& = MAPILogon(0&, vbNullString, vbNullString, 0&, 0&,
Session&)
rc& = MAPIFindNext(Se ssion&, 0&, vbNullString, vbNullString, _
MAPI_GUARANTEE_ FIFO, 0&, MessageID)

Do While rc& = MAPI_SUCCESS
rc& = MAPIReadMail(Se ssion&, 0&, MessageID$, _
MAPI_ENVELOPE_O NLY, _
0&, Msg, Originator, aRecips(), aFiles())
If rc& = MAPI_SUCCESS Then

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

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

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

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(Se ssion&, 0&, vbNullString, MessageID$, _
0&, 0&, MessageID)
Loop

rc& = MAPILogoff(Sess ion&, 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
3128
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 = chunk_split(base64_encode($file)); $email_boundary = md5(uniqid(time())); $email_headers = "MIME-Version: 1.0\r\n";
0
2449
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 3@ folowed by the name of the file that should be attached. There are 3 problems: 1) There has to be a way of doing this without all those system...
1
669
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 company's email server) receive an email from one of our vendors to which is attached a text file (a comma-delimited data file containing the details of our...
4
3430
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 second does not produce an error. The "Save" button uses "Docmd.Save" to save the record. If I insert "Me.Dirty = False" before "Docmd.Save" an...
3
4746
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 been ask to do now is to include an attached file, that when opened will be an Outlook Calendar event and when saved the event will be added to the...
3
2234
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 with a number of files. The information, including the file is stored in the database, and an email is sent with the information and any files that...
1
1407
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 emails from the inbox folder, I can catch all properties of the emails. I want to know how to save the attached file into a specific location.
4
11019
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 2005 DB. The files are of a fixed length format which map to certain elements of data that my database will exploit. If i can learn to read 1 file...
1
4867
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 program,which can upload many file in folder.problem is,am unable to save my file name in to database.because i used same name for all input file type as...
0
7468
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7401
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7757
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4945
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3450
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3443
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1884
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
704
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.