Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    talhaparvaiz@yahoo.com is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    May 2020
    Posts
    29

    Sending email through MS access

    Hi,

    I am new bee to Access and want to send emails to a multiple recipients through MS Access/Outlook. here are my requirements


    • Recipient addresses are stored in a table (about 1000 of them)
    • Message and subject will be the same for all the emails
    • want to send one recipient per email
    • want to attach word file to all the emails
    • Want to send single email after every 5 minutes until all recipients receive it
    • I want to suppress any pop-up
    • my outlook currently have 2 accounts set up. want to use one of them



    Thanks in advance for assistance

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    This is a common topic and many code examples available.

    Look at some of the threads referenced at bottom.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Note that this is cross posted.

    https://www.excelguru.ca/content.php?184
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    talhaparvaiz@yahoo.com is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    May 2020
    Posts
    29
    Quote Originally Posted by June7 View Post
    This is a common topic and many code examples available.

    Look at some of the threads referenced at bottom.



    cool, I am able to send basic email with the code below.


    Here is what I am having challenges with:


    1) email is not adding attachments
    2) rather than sending emails to fixed addresses, I want to send it to recipients saved in the table "Emails_to_send"


    can you please assist








    Option Compare Database


    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("To@email.com")
    objOutlookRecip.Type = olTo


    ' Add the CC recipient(s) to the message.
    Set objOutlookRecip = .Recipients.Add("CC@email.com")
    objOutlookRecip.Type = olCC


    ' Set the Subject, Body, and Importance of the message.
    .Subject = "This is an Automation test with Microsoft Outlook"
    .Body = "Last test - I promise." & 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

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,912

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    I never used Attachment object. Simple example, dress it up as much as you want:
    Code:
    With objOutlookMsg
            .To = "some email"
            ''.cc = ""
            ''.bcc = ""
            .Attachments.Add "filepath\filename"
            .Subject = "Test"
            .Display
            .body = "Test " & vbCrLf & "multi-line" & vbCrLf & " message."
        '    .Send
    End With
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    talhaparvaiz@yahoo.com is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    May 2020
    Posts
    29
    Quote Originally Posted by June7 View Post
    I never used Attachment object. Simple example, dress it up as much as you want:
    Code:
    With objOutlookMsg
            .To = "some email"
            ''.cc = ""
            ''.bcc = ""
            .Attachments.Add "filepath\filename"
            .Subject = "Test"
            .Display
            .body = "Test " & vbCrLf & "multi-line" & vbCrLf & " message."
        '    .Send
    End With


    Here is what I did

    1) modified code as below and ran it, resulting in an error message "compile error: user defined type not defined"

    Option Compare Database




    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("To@email.com")
    objOutlookRecip.Type = olTo




    ' Add the CC recipient(s) to the message.
    Set objOutlookRecip = .Recipients.Add("CC@email.com")
    objOutlookRecip.Type = olCC




    ' Set the Subject, Body, and Importance of the message.
    .Subject = "This is an Automation test with Microsoft Outlook"
    .Body = "Last test - I promise." & vbCrLf & vbCrLf
    .Importance = olImportanceHigh 'High importance




    ' Add attachments to the message.
    If Not IsMissing(AttachmentPath) Then
    Set objOutlookAttach = .Attachments.Add("W:\My Documents\Customers.txt")
    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





    2) found a solution to this problem @ the following site that resulted in another error


    https://www.cimaware.com/expert-zone...pe-not-defined



    "Name conflicts with the existing module, project or object library"

    I have my visual steps with this note. Please advise

    Thanks

    Click image for larger version. 

Name:	MS Access issues.jpg 
Views:	48 
Size:	53.5 KB 
ID:	41809



    Re. Cross posted... may I please know what am I doing wrong?

    Thanks

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by talhaparvaiz@yahoo.com View Post
    Re. Cross posted... may I please know what am I doing wrong?
    Did you read the link?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    talhaparvaiz@yahoo.com is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    May 2020
    Posts
    29
    Quote Originally Posted by pbaldy View Post
    Did you read the link?


    Sure, I wasn't aware of this rule.
    so that I have it right, it's okay to post same (or similar) post on multiple forums but we need to put the cross references. Please let me know

    Thanks

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Yes, it's okay to do as long as you let people know. It's very frustrating to put in a bunch of work to answer a question only to find it was answered earlier on another site.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    talhaparvaiz@yahoo.com is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    May 2020
    Posts
    29
    Quote Originally Posted by talhaparvaiz@yahoo.com View Post
    Here is what I did

    1) modified code as below and ran it, resulting in an error message "compile error: user defined type not defined"

    Option Compare Database




    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("To@email.com")
    objOutlookRecip.Type = olTo




    ' Add the CC recipient(s) to the message.
    Set objOutlookRecip = .Recipients.Add("CC@email.com")
    objOutlookRecip.Type = olCC




    ' Set the Subject, Body, and Importance of the message.
    .Subject = "This is an Automation test with Microsoft Outlook"
    .Body = "Last test - I promise." & vbCrLf & vbCrLf
    .Importance = olImportanceHigh 'High importance




    ' Add attachments to the message.
    If Not IsMissing(AttachmentPath) Then
    Set objOutlookAttach = .Attachments.Add("W:\My Documents\Customers.txt")
    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





    2) found a solution to this problem @ the following site that resulted in another error


    https://www.cimaware.com/expert-zone...pe-not-defined



    "Name conflicts with the existing module, project or object library"

    I have my visual steps with this note. Please advise

    Thanks

    Click image for larger version. 

Name:	MS Access issues.jpg 
Views:	48 
Size:	53.5 KB 
ID:	41809



    Re. Cross posted... may I please know what am I doing wrong?

    Thanks

    Any suggestions on my issues?

  12. #12
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Here is a VBA function I use to send Outlook messages, just add it to a standard module:
    Code:
    Function vcSendEmail_Outlook_With_Attachment(sSubject As String, sTo As String, Optional sCC As String, Optional sBcc As String, Optional sAttachment As String, Optional sBody As String)
        Dim OutApp As Object
        Dim OutMail As Object
     
      
        Set OutApp = CreateObject("Outlook.Application")
        
        
        OutApp.Session.Logon
      
     
        Set OutMail = OutApp.CreateItem(0)
     
        OutMail.To = sTo
        If sCC <> "" Then OutMail.CC = sCC
        If sBcc <> "" Then OutMail.BCC = sBcc
        OutMail.Subject = sSubject
        If sBody <> "" Then OutMail.HTMLBody = sBody
    
    
        OutMail.Attachments.Add (sAttachment)
    
    
        OutMail.Display  'Send | Display
        Set OutMail = Nothing
        Set OutApp = Nothing
    End Function
    To use it just call it from a loop and supply the required/optional arguments as needed.

    Based on your original requirements I think it would be much easier/faster to concatenate multiple email addresses into one semi-colon separated string and use that for the bCC of the new message, as it will appear to the recipients that they were the only one being included.

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  13. #13
    talhaparvaiz@yahoo.com is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    May 2020
    Posts
    29
    Quote Originally Posted by Gicu View Post
    Here is a VBA function I use to send Outlook messages, just add it to a standard module:
    Code:
    Function vcSendEmail_Outlook_With_Attachment(sSubject As String, sTo As String, Optional sCC As String, Optional sBcc As String, Optional sAttachment As String, Optional sBody As String)
        Dim OutApp As Object
        Dim OutMail As Object
     
      
        Set OutApp = CreateObject("Outlook.Application")
        
        
        OutApp.Session.Logon
      
     
        Set OutMail = OutApp.CreateItem(0)
     
        OutMail.To = sTo
        If sCC <> "" Then OutMail.CC = sCC
        If sBcc <> "" Then OutMail.BCC = sBcc
        OutMail.Subject = sSubject
        If sBody <> "" Then OutMail.HTMLBody = sBody
    
    
        OutMail.Attachments.Add (sAttachment)
    
    
        OutMail.Display  'Send | Display
        Set OutMail = Nothing
        Set OutApp = Nothing
    End Function
    To use it just call it from a loop and supply the required/optional arguments as needed.

    Based on your original requirements I think it would be much easier/faster to concatenate multiple email addresses into one semi-colon separated string and use that for the bCC of the new message, as it will appear to the recipients that they were the only one being included.

    Cheers,
    Here is what I did

    1) created a sub

    Option Compare Database


    Sub vcSendEmail_Outlook_With_Attachment(sSubject As String, sTo As String, Optional sCC As String, Optional sBcc As String, Optional sAttachment As String, Optional sBody As String)
    Dim OutApp As Object
    Dim OutMail As Object


    Set OutApp = CreateObject("Outlook.Application")


    OutApp.Session.Logon


    Set OutMail = OutApp.CreateItem(0)

    OutMail.To = sTo
    If sCC <> "" Then OutMail.CC = sCC
    If sBcc <> "" Then OutMail.BCC = sBcc
    OutMail.Subject = sSubject
    If sBody <> "" Then OutMail.HTMLBody = sBody




    OutMail.Attachments.Add (sAttachment)




    OutMail.Display 'Send | Display
    Set OutMail = Nothing
    Set OutApp = Nothing
    End Sub


    2) calling it from the form button click

    Option Compare Database


    Private Sub Command0_Click()




    vcSendEmail_Outlook_With_Attachment "Seeking BA opportunities", "info@test.com", "", "x", "\\fa\fahome\user\Desktop\personal\file.docx", ""




    End Sub




    Questions:
    I have my email addresses (about 1000) in a table_A, column name Email
    Q1 - Rather than using fixed email address, how can I get the email address from the table and pass it to the function
    Q2 - how can I put, let's say 20 email addresses, per email in the BCC field? believe they should be in the format such as A@email.com, B@email.com...
    Q3 - the above code is creating a new email with the data populated, but I am still required to press the send button. This is a good option for testing purposes but I want to send the email without any popups.

    Please assist.

    Thanks again for your assistance

  14. #14
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    Please post code between CODE tags to retain indentation and readability.

    Need to open a recordset object of email addresses. Then loop the recordset. The loop will either send multiple emails (each for one address) or build a concatenated string of addresses which will be used outside the loop to send one email with multiple addresses.

    Here is example of the latter https://www.vba-market.com/vba-send-...le-recipients/
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  15. #15
    talhaparvaiz@yahoo.com is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    May 2020
    Posts
    29
    Quote Originally Posted by June7 View Post
    Please post code between CODE tags to retain indentation and readability.

    Need to open a recordset object of email addresses. Then loop the recordset. The loop will either send multiple emails (each for one address) or build a concatenated string of addresses which will be used outside the loop to send one email with multiple addresses.

    Here is example of the latter https://www.vba-market.com/vba-send-...le-recipients/

    To keep it simple, I tried to extract 10 email addresses from the DB and send it to individual recipients. Here is the code that I used but getting an error message


    Calling function on the click of the button:

    Code:
    Option Compare Database
    
    Sub Command0_Click()
    
    
    Dim objRecordset As ADODB.Recordset
    Set objRecordset = New ADODB.Recordset
    Dim i As Integer
    Dim value As Variant
    
    
    initated recordset obejct
    objRecordset.ActiveConnection = CurrentProject.Connection
    objRecordset.Open ("tbl_Email")
    
    
    
    
    Dim i As Integer
    For i = 1 To 10 'sending 10 emails individually
    
    vcSendEmail_Outlook_With_Attachment "Subject", objRecordset.Fields.Item(1).value, "", "", "W:\My Documents\Customers.txt", ""
    
    
    
    
    objRecordset.MoveNext
    
    
    Next i
    End Sub
    function:


    Code:
    Option Compare Database
    
    Sub vcSendEmail_Outlook_With_Attachment(sSubject As String, sTo As String, Optional sCC As String, Optional sBcc As String, Optional sAttachment As String, Optional sBody As String)
        Dim OutApp As Object
        Dim OutMail As Object
     
      
        Set OutApp = CreateObject("Outlook.Application")
        
        
        OutApp.Session.Logon
      
     
        Set OutMail = OutApp.CreateItem(0)
     
        OutMail.To = sTo
        If sCC <> "" Then OutMail.CC = sCC
        If sBcc <> "" Then OutMail.BCC = sBcc
        OutMail.Subject = sSubject
        If sBody <> "" Then OutMail.HTMLBody = sBody
    
    
    
    
        OutMail.Attachments.Add (sAttachment)
    
    
    
    
        OutMail.Display  'Send | Display
        Set OutMail = Nothing
        Set OutApp = Nothing
    End Sub
    error message

    Click image for larger version. 

Name:	compile error.GIF 
Views:	27 
Size:	29.2 KB 
ID:	41891



    Thanks

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Sending email through access 365
    By willmafingerdo in forum Access
    Replies: 7
    Last Post: 02-18-2020, 03:23 PM
  2. Sending a value from Access to Outlook email
    By George in forum Import/Export Data
    Replies: 8
    Last Post: 09-03-2018, 02:53 AM
  3. Replies: 1
    Last Post: 11-07-2016, 11:18 AM
  4. sending email through Access
    By crowegreg in forum Import/Export Data
    Replies: 4
    Last Post: 04-15-2012, 03:49 PM
  5. sending gmail email through access
    By TheShabz in forum Programming
    Replies: 20
    Last Post: 02-19-2012, 12:24 PM

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