Solved

AddAttachments command adding multiple copies to output

Posted on 2016-10-18
4
21 Views
Last Modified: 2016-10-18
I have a function that loops through a table and sends an email with an attachment to each email address in the table.

I store the file name to a variable "Attach".

The problem I am having is that the .AddAttachment =Attach keeps adding an additional copy of the attachment every time the scripts loops through the table.

So after I get to the 20th record in the table, that email has 20 copies of the attachment with the email.

I have uploaded the script, can you tell what the problem is?

Thanks

Glen
Emailfunction.txt
0
Comment
Question by:GPSPOW
  • 2
4 Comments
 
LVL 7

Expert Comment

by:COACHMAN99
Comment Utility
attach is being set outside the loop, so never changes, and you may try initializing the imsg on each iteration, it seems to be adding the attach to the same email?
0
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 500 total points
Comment Utility
try move:

.AddAttachment Attach

Open in new window


outside and before the loop.

hence:
Public Function EmailProcedure_New()


    
 
        
        'Send E-Mail with attachment
        
    Dim Msg As String
    Dim rst As Recordset
    Dim dbs As Database
    
    Dim strSQL As String
    Dim Attach As String
    Dim FileName As String
    Dim iMsg As Object
    Dim iConf As Object
    Dim Flds As Variant
    
    strSQL = "Select Email from test_powers "
    strSQL = strSQL & "Where ID <21"
    
    Set dbs = CurrentDb
    Set rst = dbs.OpenRecordset(strSQL, dbOpenForwardOnly)
    
    
'//SMTP Configuration Settings
    
    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")
    iConf.Load -1
    Set Flds = iConf.Fields
    With Flds
    
    '//SMTP Configuration for SMTP.SLEH.COM
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") _
                        = "smtp.sleh.com"
         .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
         .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 0
         
    '//SMTP Configuration for SMTPCORP.COM
         '.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") _
                        = "smtpcorp.com"
        '.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 2525
        '.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
        '.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "gpspow"
        '.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "GP0890"
        .Update
    End With
    
    Attach = "\\pmcfs\groups\accounting\CEO_Rpts\404a-5 10-5-16" & ".PDF"
    iMsg.AddAttachment Attach
    
        
       Do While Not rst.EOF
       
        
        
    
   
        Dim Subj As String
        Dim EmailAddr As String
        Dim Recipient As String
        Dim CarbonCopy As String
        
        
        
        'Get the data
        
        
        Subj = "2016 401K Plan & Investment Notice"
       
        EmailAddr = rst.Fields("Email")
        
        
        
        'Compose Message
        Msg = "Please see the attached file containing important information about the " & vbCrLf
        Msg = Msg & "Patients Medical Center 401(k) Plan.  " & vbCrLf & vbCrLf
        Msg = Msg & "Do not hesitate to contact Katrina Gallegos at 713-948-7083, " & vbCrLf
        Msg = Msg & "if you have questions." & vbCrLf & vbCrLf & vbCrLf
        Msg = Msg & "Human Resources " & vbCrLf
        Msg = Msg & "St. Luke's Patient's Medical Center" & vbCrLf
        Msg = Msg & "4600 E. Sam Houston Parkway" & vbCrLf
        Msg = Msg & "Pasadena, TX  77505" & vbCrLf
        Msg = Msg & "713-948-7083" & vbCrLf
       
        
           
    '//Set EMAIL Settings
            
            

            With iMsg
                Set .Configuration = iConf
                .To = EmailAddr
                .From = "gpowers@stlukeshealth.org"
                .BCC = ""
                .Subject = Subj
                .TextBody = Msg
                .Fields("urn:schemas:mailheader:return-receipt-to") = "gpowers@stlukeshealth.org"
            .DSNOptions = 14
            .Fields.Update            

                .Send
            End With

    rst.MoveNext
    Loop
    rst.Close
    Set rst = Nothing
    Set dbs = Nothing


end function

Open in new window

or you got to clear the attachment before adding a new one in your loop.
0
 
LVL 7

Expert Comment

by:COACHMAN99
Comment Utility
The problem is adding the same attachment 20 times to the same email.
0
 

Author Closing Comment

by:GPSPOW
Comment Utility
thanks

That worked just fine.

Glen
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now