Solved

send auto-mail with file attachment from local drive

Posted on 2001-09-03
6
335 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

752 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