Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

AddAttachments command adding multiple copies to output

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

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

704 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