Email Problem

cuttsy

The Great Pretender
Local time
Today, 20:00
Joined
Jun 9, 2004
Messages
164
I used some code that I got from this forum to send an email. It worked fine.

Code:
Function SendWithOutlook()
On Error GoTo Error_Handler

    Dim objOutlook As Outlook.Application
    Dim objMsg As Outlook.MailItem
    Dim objRecipient As Outlook.Recipient
    Dim objMailAttachment As Outlook.Attachment
    

    Dim Attach1 As String
    Dim Attach2 As String
    Dim Attach3 As String
    Dim Attach4 As String
    Dim Rec As String
    Dim Subj As String
    Dim Main As String
        

    Set objOutlook = CreateObject("Outlook.Application")
    Set objMsg = objOutlook.CreateItem(olMailItem)
    
    Attach1 = ""
    Attach2 = ""
    Attach3 = ""
    Attach4 = ""

    With objMsg
        .To = "r.cutts@tees.ac.uk"
        .Subject = "subject of e-mail"
        .Body = "body goes here"

        If Attach1 <> "" Then
            Set objMailAttachment = .Attachments.Add(Attach1)
        End If
        If Attach2 <> "" Then
            Set objMailAttachment = .Attachments.Add(Attach2)
        End If
        If Attach3 <> "" Then
            Set objMailAttachment = .Attachments.Add(Attach3)
        End If
        If Attach4 <> "" Then
            Set objMailAttachment = .Attachments.Add(Attach4)
        End If
            
        .send
    End With
     
    Set objOutlook = Nothing
    Set objMsg = Nothing
    Set objRecipient = Nothing
    Set objMailAttachment = Nothing

Error_Handler:
   If Err.Number = "287" Then
      MsgBox "You clicked No to the Outlook security warning. " & _
      "Rerun the procedure and click Yes to access e-mail" & _
      "addresses to send your message. "
   End If

End Function

Now I wanted to be able to send it to all of the contacts in a query with an email address so I inserted code that I hoped would achieve this. The Whole code looks like this:


Code:
Function SendWithOutlook()
On Error GoTo Error_Handler

    Dim objOutlook As Outlook.Application
    Dim objMsg As Outlook.MailItem
    Dim objRecipient As Outlook.Recipient
    Dim objMailAttachment As Outlook.Attachment
    

    Dim Attach1 As String
    Dim Attach2 As String
    Dim Attach3 As String
    Dim Attach4 As String
    Dim Rec As String
    Dim Subj As String
    Dim Main As String

    '---------------------------------------------
    'COMPILE THE "TO:" LIST FROM THE MASTER QUERY
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim strSQL As String
    Dim strSELECT As String
    Dim strFROM As String
    Dim strORDER As String
    Dim strWHERE As String
    
    Set db = CurrentDb
    
    strSELECT = "SELECT Master.fName, Master.sName, Master.SchoolName, Master.email "
    strFROM = "FROM Master "
    strORDER = "ORDER BY Master.sName;"
    strWHERE = "WHERE Master.email Is Not Null "
    
    strSQL = strSELECT & strFROM & strWHERE & strORDER
    
    Set rs = db.OpenRecordset(strSQL)
    
    rs.MoveFirst
        Rec = rs.Fields("email")
        rs.MoveNext
        Do While Not rs.EOF
            Rec = Rec & ", " & rs.Fields("email")
            rs.MoveNext
        Loop
    '#############################################
    '#############################################
        

    Set objOutlook = CreateObject("Outlook.Application")
    Set objMsg = objOutlook.CreateItem(olMailItem)
    
    Attach1 = ""
    Attach2 = ""
    Attach3 = ""
    Attach4 = ""

    With objMsg
        .To = "r.cutts@tees.ac.uk"
        .Subject = "subject of e-mail"
        .Body = "body goes here"

        If Attach1 <> "" Then
            Set objMailAttachment = .Attachments.Add(Attach1)
        End If
        If Attach2 <> "" Then
            Set objMailAttachment = .Attachments.Add(Attach2)
        End If
        If Attach3 <> "" Then
            Set objMailAttachment = .Attachments.Add(Attach3)
        End If
        If Attach4 <> "" Then
            Set objMailAttachment = .Attachments.Add(Attach4)
        End If
            
        .send
    End With
     
    Set objOutlook = Nothing
    Set objMsg = Nothing
    Set objRecipient = Nothing
    Set objMailAttachment = Nothing

Error_Handler:
   If Err.Number = "287" Then
      MsgBox "You clicked No to the Outlook security warning. " & _
      "Rerun the procedure and click Yes to access e-mail" & _
      "addresses to send your message. "
   End If

End Function

The email no longer sends. The query contains 1 record with the same email in it as the test one used before. This time I do not get the Yes/No message from outlook about sending the message. So I assume that it is nothing to do with the format of the Rec string.

Has anyone any idea what the added code is doing to prevent the email being sent.

I would appriciate and help in this rather lengthy question.

Thanks
 
Last edited:
I deleted it and re-typed it and it is working. Must have been a spelling / syntax error.
 

Users who are viewing this thread

Back
Top Bottom