Solved

AddAttachments command adding multiple copies to output

Posted on 2016-10-18
4
45 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 52

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

751 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