send auto-mail with file attachment from local drive

Posted on 2001-09-03
Last Modified: 2010-08-05
My Problem:
I have to take a file from Disk (Drive C:\) and take this file as an attachment to an mail and send it automatical.

Question by:Schobert
  • 4
LVL 12

Accepted Solution

Paurths earned 300 total points
ID: 6450413
hi Schobert,

u can use the open/save dialog to pick the file, u'll find an example here :

and here is the code for sending email with attachment: (in this example the retrieved file is strFileSelected)

Private Sub cmdSending_Click()
Dim rs As DAO.Recordset
Dim strMsg As String
Dim strSub As String

    strMsg = "Type the email body..."
    strSub = "Type the subject for the message here..."

    SendMessage "", strMsg, strSub, strFileSelected
End Sub

Private Sub SendMessage(strTo As String, strMsg As String, strSub As String, Optional AttachmentPath)
  Dim objOutlook As Outlook.Application
  Dim objOutlookMsg As Outlook.MailItem
  Dim objOutlookRecip As Outlook.Recipient
  Dim objOutlookAttach As Outlook.Attachment

  ' Create the Outlook session.
  Set objOutlook = CreateObject("Outlook.Application")

  ' Create the message.
  Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

  With objOutlookMsg
     ' Add the To recipient(s) to the message.
    Set objOutlookRecip = .Recipients.Add(strTo)
     objOutlookRecip.Type = olTo

     ' Set the Subject, Body, and Importance of the message.
     .Subject = strSub
     .Body = strMsg & vbCrLf & vbCrLf
     .Importance = olImportanceHigh  'High importance

     ' Add attachments to the message.
     If Not IsMissing(AttachmentPath) Then
        Set objOutlookAttach = .Attachments.Add(AttachmentPath)
     End If

     ' Resolve each Recipient's name.
     For Each objOutlookRecip In .Recipients
        If Not objOutlookRecip.Resolve Then
     End If

  End With
  Set objOutlookMsg = Nothing
  Set objOutlook = Nothing

End Sub

hope this  helps,
LVL 12

Expert Comment

ID: 6450426
btw, u should adjust the open/save function so that it will return the path and name of the selected file:

Private Sub cmdSending_Click()
Dim strMsg As String
Dim strSub As String
dim strFileSelected as variant

   strFileSelected = TestIt()
   strMsg = "Type the email body..."
   strSub = "Type the subject for the message here..."

   SendMessage "", strMsg, strSub, strFileSelected
End Sub

Function TestIt() as variant
    Dim strFilter As String
    Dim lngFlags As Long
    strFilter = ahtAddFilterItem(strFilter, "Access Files (*.mda, *.mdb)", _
    strFilter = ahtAddFilterItem(strFilter, "dBASE Files (*.dbf)", "*.DBF")
    strFilter = ahtAddFilterItem(strFilter, "Text Files (*.txt)", "*.TXT")
    strFilter = ahtAddFilterItem(strFilter, "All Files (*.*)", "*.*")
    MsgBox "You selected: " & ahtCommonFileOpenSave(InitialDir:="C:\", _
        Filter:=strFilter, FilterIndex:=3, Flags:=lngFlags, _
        DialogTitle:="Hello! Open Me!")
    ' Since you passed in a variable for lngFlags,
    ' the function places the output flags value in the variable.
    Debug.Print Hex(lngFlags)
End Function
LVL 12

Expert Comment

ID: 6450434
u can also adjust the filter set in the dialog (and an adjustment to the returning value that i forgot...)
only choosing excel files:

Function TestIt() As Variant
    Dim strFilter As String
    Dim lngFlags As Long
    strFilter = ahtAddFilterItem(strFilter, "Excel Files (*.xls)", _
    TestIt = ahtCommonFileOpenSave(InitialDir:=strLoc, _
        Filter:=strFilter, FilterIndex:=3, Flags:=lngFlags, _
        DialogTitle:="Hello! Open Me!")

End Function
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.


Expert Comment

ID: 6451771
What email program are you using? Does it have to serve various mail programs?

This is actually a fairly simple one. Answer the above two questions and I'll give you some code that does it.

Author Comment

ID: 6452253
It was possilbe to solve the problem as I have seen Your proposal, but the code worked not emediately on my computer ( I thinke it depends not on Your code).
Thanke You for Your quick help !
best regards,
Sigi Schobert
LVL 12

Expert Comment

ID: 6452514
hi Schobert,

glad it helped u with sending those emails,
i am wondering a little why this code is only worth a B-grade.
It is excellent code i provided.
Please dont feel like this is attacking you in anyway, it is not.
I am just curious.


Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Some sers suddenly getting error popup msg 28 85
Access 2007 Switch To/Retry/Cancel Pop-Up 6 30
A Function to parse a text string 4 34
Access Open Report with SQL Parameter 11 28
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now