Results 1 to 2 of 2
  1. #1
    Paul Ager is offline Novice
    Windows 2K Access 2000
    Join Date
    Mar 2011
    Posts
    3

    Trying to send an email from Query results and add attachment


    Hi,

    I hope someone can help. I'm trying to add an attachment to an email from email addresses that are generated from a query. I've tried to amalgamate two pieces of code to form one but I am getting the Complie Error: 'End If without block If' message. I'd really appreciate it if someone could cast their expert eye over the code below and see if they can find a solution.

    Code:
     
    Private Sub email_exp1_Click() 
        On Error Goto Err_email_exp1_Click 
         
        Dim MyDb As DAO.Database 
        Dim rsEmail As DAO.Recordset 
        Dim qdf As DAO.QueryDef 
        Dim prm As DAO.Parameter 
        Dim strEmail As String 
        Dim strMsg As String 
        Dim oLook As Object 
        Dim oMail As Object 
    
        Set MyDb = CurrentDb 
        Set qdf = MyDb.QueryDefs("qryexp_may11") 
         
        For Each prm In qdf.Parameters 
            prm.Value = Eval(prm.Name) 
        Next 
         
        Set rsEmail = qdf.OpenRecordset() 
        Set oLook = CreateObject("Outlook.Application") 
         
        With rsEmail 
            .MoveFirst 
            Do Until rsEmail.EOF 
                If IsNull(.Fields(0)) = False Then 
                     
                    Set oMail = oLook.createitem(0) 
                    With oMail 
                        .to = .Fields(0) 
                        .body = "See attached" 
                        .Subject = "Is the file attached" 
                        .Attachments.Add("\\mynetwork\mynetworkfolder\example.doc") 
                        .Send 
                    End If 
                    .MoveNext 
                Loop 
            End With 
             
            Set oMail = Nothing 
            Set oLook = Nothing 
             
    Err_email_exp1_Click: 
            MsgBox Err.Description 
            Resume Exit_email_exp1_Click 
             
        End Sub
    I've been studying this code for a while and cannot see anyway forward. Any help would be great!

    Paul

  2. #2
    Paul Ager is offline Novice
    Windows 2K Access 2000
    Join Date
    Mar 2011
    Posts
    3
    Eureka! I found the solution, please see working code below.

    Code:
     
    Private Sub email_exp1_Click()
     
    Dim MyDb As DAO.Database
    Dim rsEmail As DAO.Recordset
    Dim qdf As DAO.QueryDef
    Dim prm As DAO.Parameter
    Dim strEmail As String
    Dim strMsg As String
    Dim oLook As Object
    Dim oMail As Object
    Set MyDb = CurrentDb
    Set qdf = MyDb.QueryDefs("qryexp_may11")
    For Each prm In qdf.Parameters
        prm.Value = Eval(prm.Name)
    Next
    Set rsEmail = qdf.OpenRecordset()
    Set oLook = CreateObject("Outlook.Application")
    With rsEmail
            .MoveFirst
            Do Until rsEmail.EOF
            myRecipient = .Fields(0)
                If IsNull(myRecipient) = False Then
                    Set oLook = CreateObject("Outlook.Application")
                    Set oMail = oLook.createitem(0)
                    With oMail
                        .to = myRecipient
                        .body = "See attached"
                        .Subject = "Test Email"
                        .Attachments.Add ("\\mynetwork\mynetworkfolder\test.doc")
                        .Send
                    End With
                End If
                        .MoveNext
            Loop
    End With
    Set oMail = Nothing
    Set oLook = Nothing
    End Sub
    Thanks again!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. send email to email addresses in database?
    By cnstarz in forum Access
    Replies: 5
    Last Post: 03-02-2011, 09:46 PM
  2. Send in Email Body vice Attachment
    By cassidym in forum Programming
    Replies: 2
    Last Post: 10-20-2010, 06:21 AM
  3. Email Query Results
    By eddie_keating in forum Queries
    Replies: 1
    Last Post: 06-16-2010, 11:09 AM
  4. Beginner trying to send email with attachment
    By ahm in forum Programming
    Replies: 2
    Last Post: 03-24-2009, 08:51 PM
  5. email attachment from Access
    By Gandalf in forum Queries
    Replies: 0
    Last Post: 01-22-2009, 10:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums