Solved

AddAttachments command adding multiple copies to output

Posted on 2016-10-18
4
28 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
ID: 41849399
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
ID: 41849404
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
ID: 41849407
The problem is adding the same attachment 20 times to the same email.
0
 

Author Closing Comment

by:GPSPOW
ID: 41849410
thanks

That worked just fine.

Glen
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

947 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

20 Experts available now in Live!

Get 1:1 Help Now