Results 1 to 7 of 7
  1. #1
    StuartR is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Location
    Scotland
    Posts
    46

    Question Best way to send out a batch email using VBA

    Needing some pointers here if possible ...



    I have a successfully working section on a membership database system which merges word documents, creates the mail merge, and prints out the letters. All this works fine, and I attach below the programme etc that works.

    There is a need however to potentially send out these letters or documents (or a similar version) to those members where we have an email address, and thus avoid postage costs etc. We are probably looking at approx. 500 members, (so not huge numbers)

    What is the best way to achieve this, ideally, just operating it from a button on a form (as per the mail merge) so that the users do not need to go into the "back shop" of the Access workings. Is it possible to do something very similar using "Words" email function ?

    Any guidance would be appreciated.

    The current mail merge programme is as follows ...

    Code:
    Private Sub SetQuery(strQueryName As String, strSQL As String)
    On Error GoTo ErrorHandler
    
    'set the query from which the merge document will pull its info
    Dim qdfNewQueryDef As QueryDef
    Set qdfNewQueryDef = CurrentDb.QueryDefs(strQueryName)
    qdfNewQueryDef.SQL = strSQL
    qdfNewQueryDef.Close
    RefreshDatabaseWindow
    
    Exit Sub
    
    ErrorHandler:
    MsgBox "Error #" & Err.Number & " occurred." & Err.Description, vbOKOnly, "Error"
    Exit Sub
    
    End Sub
    
    
    
    
    Private Sub LapsedBttn_Click()
    Dim qdf As DAO.QueryDef
    Dim db As DAO.Database
    
    Dim strSQL As String
    Dim strDataSrc As String
    Dim strTemplate As String
    
    strDataSrc = "C:\Users\User5\Documents\LUCS\TestData.csv"
    strTemplate = "C:\Users\User5\Documents\LUCS\Lapsed_Reminder.docx"
    
    Set db = CurrentDb
    
    strSQL = "SELECT Member_Names.Membership_Number, Member_Names.Status, Member_Names.First_Name, Member_Names.[Full Name], Member_Names.Membership_Class, Member_Names.[Address Line 1], Member_Names.[Address Line 2], Member_Names.Town, Member_Names.Region, Member_Names.Country, Member_Names.[Post Code], Member_Names.Last_Payment FROM Member_Names  WHERE (((Member_Names.Membership_Number) Not Like 'H/*') AND ((Member_Names.Status)='Current') AND ((Member_Names.Membership_Class)<> 'Family (Member)' And (Member_Names.Membership_Class)<> 'Life') AND ((Year([Last_Payment]))<Year(Date())-1)) OR (((Member_Names.Membership_Number) Not Like 'H/*') AND ((Member_Names.Status)='Current') AND ((Member_Names.Membership_Class)<> 'Family (Member)' And (Member_Names.Membership_Class)<> 'Life') AND ((Member_Names.Last_Payment) Is Null))"
    
    
    'set the query from which the merge document will pull its info
    On Error Resume Next
    With db
    .QueryDefs.Delete ("qData")
    On Error GoTo 0
    Set qdf = .CreateQueryDef("qData", strSQL)
    End With
    
    Set qdf = Nothing
    Set db = Nothing
    
    
    
    'Create the csv from the query
    DoCmd.TransferText acExportDelim, , "qData", strDataSrc, True
    
    'Do the mail merge
    
    Call fcnWordMergeMethod2(strTemplate, strDataSrc)
    End Sub
    
    
    
    Function fcnWordMergeMethod2(strWordTemplate As String, strDataSource As String)
    On Error GoTo fcnWordMergeMethod2_Error
    
    Dim strDocName As String
    Dim wActiveDoc As Object
    Dim appWord As Object
    Dim wDoc As Object
    
    Const wdDoNotSaveChanges = 0
    Const wdSEndToNewDocument = 0
    Const wdDialogMailMerge = 676
    
    ' Open a new mail merge document based on the selected template
    
    Set appWord = GetObject(Class:="Word.Application")
    
    'Close any existing open documents
    
    For Each wDoc In appWord.Documents
    wDoc.Close wdDoNotSaveChanges
    Next wDoc
    
    appWord.Documents.Add strWordTemplate
    appWord.Visible = True
    strDocName = appWord.ActiveDocument.Name
    
    'Set the Merge Data source to the csv file and do the merge
    
    With appWord
    .Activate
    .ActiveDocument.MailMerge.OpenDataSource Name:=strDataSource
    .ActiveDocument.MailMerge.Destination = wdSEndToNewDocument
    .ActiveDocument.MailMerge.Execute
    .Dialogs(wdDialogMailMerge).Show
    
    'Save the newly created merge document
    '.ActiveDocument.SaveAs strSaveNamePath
    
    'Close the master merge document
    .Documents(strDocName).Close SaveChanges:=wdDoNotSaveChanges
    End With
    
    Set wActiveDoc = appWord.ActiveDocument
    
    fcnWordMergeMethod2_Exit:
    Exit Function
    
    fcnWordMergeMethod2_Error:
    Select Case Err
    Case 429
    Set appWord = CreateObject(Class:="Word.Application")
    Resume Next
    Case 4605, 5132
    Resume Next
    Case 5174
    MsgBox "File " & strWordTemplate & " not found"
    Case Else
    MsgBox Err.Number & ", " & Err.Description & " Procedure fcnWordMergeMethod2" & " of basWordCode"
    End Select
    
    Resume fcnWordMergeMethod2_Exit
    
    End Function

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    in Access:
    docmd.SendObject acSendReport ,"rMyReport",acFormatPDF,sTo, ,,sSubj,sBody
    or
    docmd.SendObject acSendQuery ,"qsMyQuery",acFormatExcel,sTo,,,sSubj,sBody

  3. #3
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    Also well worth looking at is the 'Super Easy Mail Merge' feature by Albert Kallal
    http://www.kallal.ca/msaccess/msaccess.html
    Many people including myself have used this successfully for many years - it really is easy to use

    One word of warning - if you are sending out 500 emails, you may get issues with it being identified as junk email.
    You may need to limit the numbers of emails sent per minute or use special software to do this for you
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  4. #4
    StuartR is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Location
    Scotland
    Posts
    46

    Why am I getting a "can't send" message ....

    Quote Originally Posted by ranman256 View Post
    in Access:
    docmd.SendObject acSendReport ,"rMyReport",acFormatPDF,sTo, ,,sSubj,sBody
    or
    docmd.SendObject acSendQuery ,"qsMyQuery",acFormatExcel,sTo,,,sSubj,sBody
    Ranman ~ Thanks for your quick response, much appreciated.

    I have entered the following code, but I am getting a Runtime error 2293 cant send this email message. Any Clues ???

    Code:
    Private Sub Form_AfterUpdate()
    
    Dim varName As Variant
    Dim varCC As Variant
    Dim varSubject As Variant
    Dim varBody As Variant
    
    varName = "ezine@lucs.org.uk"
    varCC = "marketing@lucs.org.uk"
    varSubject = "New Member"
    varBody = "The following member has just joined the Society" & vbCrLf & Me.First_Name + " " + Me.Last_Name
    
    DoCmd.SendObject acSendNoObject, , , varName, varCC, , varSubject, varBody, True, False
    
    
    End Sub

  5. #5
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    This seems to be related to new Outlook security measures and according to one only happens when Outlook is open (you could test this). There are other methods for sending emails in VBA, such as using SMTP which does not use Outlook.

  6. #6
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    Ridders52: have you tried that link lately? Seems something has moved.
    new Outlook security measures
    Not really. The problem arose about 14 years ago.
    SturartR: There are work arounds listed here if you must use SendObject. I have used CDO in the past - might be a bit daunting for some.
    Here's a list of limitations for the SendObject method that might make you change your mind (I'm not advocating the use of any solutions offered on this site)
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    Quote Originally Posted by Micron View Post
    Ridders52: have you tried that link lately? Seems something has moved.
    Not really. The problem arose about 14 years ago.
    SturartR: There are work arounds listed here if you must use SendObject. I have used CDO in the past - might be a bit daunting for some.
    Here's a list of limitations for the SendObject method that might make you change your mind (I'm not advocating the use of any solutions offered on this site)
    I checked the link before posting.
    The Access 2000(!) merge code link still works though the web page looks a couple of decades out of date....
    However, I think other sites also have Albert's code
    I have a version from around 2008 though no idea where I got it from

    There's another interesting mil merge helper utility here http://www.gmayor.com/ManyToOne.htm

    FWIW I also use CDO for sending email direct from Access apps and personally find it much easier to use than other methods
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

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

Similar Threads

  1. Replies: 3
    Last Post: 12-28-2015, 04:11 PM
  2. Replies: 5
    Last Post: 09-14-2015, 07:24 AM
  3. Replies: 5
    Last Post: 05-07-2014, 09:25 AM
  4. Outlook batch email PDF
    By Ruegen in forum Reports
    Replies: 43
    Last Post: 12-12-2013, 10:55 PM
  5. Replies: 4
    Last Post: 04-13-2011, 10:11 AM

Tags for this Thread

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