Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

AddAttachments command adding multiple copies to output

Posted on 2016-10-18
4
Medium Priority
?
59 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 54

Accepted Solution

by:
Ryan Chong earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

824 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