Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2003
    Cleveland USA

    Unhappy Unanswered: How do I email a text file from code?

    In my code, I create a text file. Now, I want to be able to email the file. From what I've seen, DoCmd.SendObject will not work because I want to send an external text file, not anything directly from the database. The text file is created from the database, but it also includes a header and footer that can't be done by simply exporting a table or report. I'm using Access 2002 right now, but I found out today that it'll end up on another network that's using 97.

  2. #2
    Join Date
    Sep 2003
    This thread has lots of info on emailing from access...

    The search functionality is fabulous on this board

  3. #3
    Join Date
    Oct 2004
    i use a program called smtpsend (if you do a search on the web you'll find it). then you can create a text file and send it using a shell command. i can send you sample code if you'd like.

  4. #4
    Join Date
    Mar 2004
    Oakland, CA, USA

    RE: Email with file attachment

    Hello LisaChow,

    I just post this thread in ASP forum. Just include the revise for your convenience. Hope it will help.

    'This code belongs to declaration section
    Option Compare Database
    Option Explicit

    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
    ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Long

    ' This code belongs to command button
    Private Sub cmdSendEmail_Click()
    Dim strSubject As String, strEmail As String, strMessage As String
    Dim ret
    Dim strFile

    strFile = "c:\test.txt"

    strSubject = "Testing email"
    strEmail = ""
    strMessage = "Hello there, this is a test"
    ShellExecute Me.hwnd, "Open", "mailto:" & strEmail & "?subject=" & strSubject & "&body=" & strMessage, vbNullString, vbNullString, vbNormalFocus
    'Wait for Outlook Express is ready
    While ret = 0
    ret = FindWindow(vbNullString, strSubject)
    SendKeys "%ia" & strFile & "{TAB 2}~" 'insert attachment
    SendKeys "%s" 'Sending email
    End Sub

  5. #5
    Join Date
    Aug 2003


    Much simpler than all of these:

    Add the Microsoft Outlook reference to the References option while viewing the code (Tools; References)

    Dim myMsg as string
    Dim myEmail as Outlook.mailitem

    set myEmail = CreateObject("Outlook.Application").CreateItem(olM ailItem)

    myEmail.To = ""
    myEmail.Subject = "Test Email"
    myEmail.Body = "Hi and Goodbye"

    Hope this helps


  6. #6
    Join Date
    Mar 2004
    Oakland, CA, USA

    RE: Email program

    Hello LisaChow,

    I use Outlook Express and Sugarflux use Microsoft Outlook as email client. You must settle down one or another.

  7. #7
    Join Date
    Aug 2002
    This works for me, not my code got it from somewhere a while back but works a treat.
    put this in a module and then call when you want.

    Sub SendMessage(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("whoever")
          objOutlookRecip.Type = olTo
          ' Add the CC recipient(s) to the message.
          Set objOutlookRecip = .Recipients.Add("whoever")
          objOutlookRecip.Type = olCC
          ' Set the Subject, Body, and Importance of the message.
          .Subject = "Test"
          .Body = "yada yada yada." & 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
        MsgBox " Your Order has been sent"
       End With
       Set objOutlookMsg = Nothing
       Set objOutlook = Nothing
    End Sub
    Last edited by bobnz; 10-28-04 at 01:09. Reason: clarity

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts