Solved

send auto-mail with file attachment from local drive

Posted on 2001-09-03
6
331 Views
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.


0
Comment
Question by:Schobert
  • 4
6 Comments
 
LVL 12

Accepted Solution

by:
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 : http://www.mvps.org/access/api/api0001.htm


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 "person@where.com", 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
        objOutlookRecip.Resolve
        If Not objOutlookRecip.Resolve Then
        objOutlookMsg.Display
     End If
     Next
     .Send

  End With
  Set objOutlookMsg = Nothing
  Set objOutlook = Nothing

End Sub


hope this  helps,
Ricky
0
 
LVL 12

Expert Comment

by:Paurths
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 "person@where.com", strMsg, strSub, strFileSelected
           
End Sub





Function TestIt() as variant
    Dim strFilter As String
    Dim lngFlags As Long
    strFilter = ahtAddFilterItem(strFilter, "Access Files (*.mda, *.mdb)", _
                    "*.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
0
 
LVL 12

Expert Comment

by:Paurths
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)", _
                    "*.xls")
    TestIt = ahtCommonFileOpenSave(InitialDir:=strLoc, _
        Filter:=strFilter, FilterIndex:=3, Flags:=lngFlags, _
        DialogTitle:="Hello! Open Me!")

End Function
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 2

Expert Comment

by:JamesMayfield
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.
0
 

Author Comment

by:Schobert
ID: 6452253
Hi,
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
0
 
LVL 12

Expert Comment

by:Paurths
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.

cheers
Ricky
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

920 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