Emailing different attachments to different email addresses

pfourn1

New Member
Joined
Oct 11, 2017
Messages
5
I am working on a spreadsheet that will send emails and their attachments to any vendor that has a "yes" in row H. The file path/name of the attachment is stored in row G. I can get it to work if I just do one at a time, but once I try to use a loop to generate all emails and attachments at one time, it bombs out. If I remove the .Attachments.Add MailAttachments line in the macro, it generates all of the emails perfectly. Any help with the attachments issue would be greatly appreciated. The Macro is below:


Code:
Sub PC_Email()


    Dim OutApp As Object
    Dim OutMail As Object
    Dim strbody As String
    Dim MailAttachments As String
    
    
    Sheets("Master").Select
    Range("A1").Select
    
    Application.ScreenUpdating = False
    Set OutApp = CreateObject("Outlook.Application")
    
  
   On Error GoTo cleanup
    For Each cell In Columns("C").Cells
        If cell.Value Like "?*@?*.?*" And _
        LCase(Cells(cell.Row, "H").Value) = "yes" Then
        
        
    With Application.ActiveSheet
        MailAttachments = Cells(cell.Row, "G").Value
    End With
        
    
    Set OutMail = OutApp.CreateItem(0)
        
            On Error Resume Next
                              
            With OutMail
            
            strbody = "Hi " & Cells(cell.Row, "B") & "," & vbNewLine & vbNewLine & _
              "The " & Cells(cell.Row, "A") & " ACH Remittance for " & Cells(cell.Row, "D") & " is attached." & vbNewLine & _
              "Please let me know if you have any questions." & vbNewLine & vbNewLine & _
              "Thanks," & vbNewLine & vbNewLine & _
              "Accounts Payable" & vbNewLine & "Reily Foods"
              
                .To = cell.Value
                .Subject = Cells(cell.Row, "A") & " ACH Remittance"
                .Body = strbody
               
                .Attachments.Add MailAttachments
                
                .Display  'Or use .Send
                  
                
            End With
            On Error GoTo 0
            Set OutMail = Nothing
        End If
    Next cell


cleanup:
    Set OutApp = Nothing
    Application.ScreenUpdating = True
    
End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
What error do you get exactly?

and how are the filenames written in the cells where they are picked from?
 
Upvote 0
.
Here is the corrected version :

Code:
Option Explicit


Sub PC_Email()




    Dim OutApp As Object
    Dim OutMail As Object
    Dim strbody As String
    Dim MailAttachments As String
    Dim cell As Variant '                             Not previously DIM'd
    
    Sheets("Master").Select '                         Edit as required
    Range("A1").Select
    
    Application.ScreenUpdating = False
    Set OutApp = CreateObject("Outlook.Application")
    
  
   On Error GoTo cleanup
    For Each cell In Columns("C").Cells
        If cell.Value Like "?*@?*.?*" And _
        LCase(Cells(cell.Row, "H").Value) = "yes" Then
        
        
    With Application.ActiveSheet
        MailAttachments = Cells(cell.Row, "G").Value
    End With
        
    
    Set OutMail = OutApp.CreateItem(0)
        
            On Error Resume Next
                              
            With OutMail
            
            strbody = "Hi " & Cells(cell.Row, "B") & "," & vbNewLine & vbNewLine & _
              "The " & Cells(cell.Row, "A") & " ACH Remittance for " & Cells(cell.Row, "D") & " is attached." & vbNewLine & _
              "Please let me know if you have any questions." & vbNewLine & vbNewLine & _
              "Thanks," & vbNewLine & vbNewLine & _
              "Accounts Payable" & vbNewLine & "Reily Foods"
              
                .To = cell.Value
                .Subject = Cells(cell.Row, "A") & " ACH Remittance"
                .Body = strbody
               
                .Attachments.Add MailAttachments
                
                .Display  'Or use .Send
                  
                
            End With
            On Error GoTo 0
            Set OutMail = Nothing
        End If
    Next cell


cleanup:
    Set OutApp = Nothing
    Application.ScreenUpdating = True
    
End Sub
 
Last edited:
Upvote 0
.
Disregard .................................
 
Last edited:
Upvote 0
I am not getting an error, it just skips to "On Error go to 0" and nothing is ever displayed.

Here is an example of a filename:
[TABLE="width: 864"]
<tbody>[TR]
[TD]G:\Accts Payable\ACH Remittance\vendor ACH 09.05.17\_952017_paymentadvice.pdf[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
.
Having problems getting the sheet view to post correctly.
 
Last edited:
Upvote 0
.

Does the file name specifically use the underscores ?
Code:
[COLOR=#333333]\_952017_paymentadvice.pdf[/COLOR]


Or is there just a blank space where the underscores are located ?
Check the spelling of the entire path and the file to verify it is accurate.

The corrected version of the macro works well here. This is an example of the sheet layout where data is
inputted to the various cells:

Excel 2007 32 bit
[TABLE="class: head"]
<tbody>[TR="bgcolor: #888888"]
[TH][/TH]
[TH]
A
[/TH]
[TH]
B
[/TH]
[TH]
C
[/TH]
[TH]
D
[/TH]
[TH]
E
[/TH]
[TH]
F
[/TH]
[TH]
G
[/TH]
[TH]
H
[/TH]
[/TR]
[TR="bgcolor: #FFFFFF"]
[TD="bgcolor: #888888"]
1
[/TD]
[TD]Laptop[/TD]
[TD]Mr. Me[/TD]
[TD]me@yahoo.com[/TD]
[TD]Asus 101[/TD]
[TD][/TD]
[TD][/TD]
[TD]C:\Users\My\Desktop\Laptops.txt[/TD]
[TD]yes[/TD]
[/TR]
[TR="bgcolor: #FFFFFF"]
[TD="bgcolor: #888888"]
2
[/TD]
[TD]Books[/TD]
[TD]Mr. You[/TD]
[TD]you@yahoo.com[/TD]
[TD]Dell BDA[/TD]
[TD][/TD]
[TD][/TD]
[TD]C:\Users\My\Desktop\Book1.xlsx[/TD]
[TD]yes[/TD]
[/TR]
[TR="bgcolor: #FFFFFF"]
[TD="bgcolor: #888888"]
3
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR="bgcolor: #FFFFFF"]
[TD="bgcolor: #888888"]
4
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR="bgcolor: #FFFFFF"]
[TD="bgcolor: #888888"]
5
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
[TABLE="class: grid"]
<tbody>[TR]
[TD]Sheet: Sheet1[/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited:
Upvote 0
[TABLE="width: 2478"]
<colgroup><col><col><col><col><col><col><col><col></colgroup><tbody>[TR]
[TD]Vendor Name[/TD]
[TD]Address To[/TD]
[TD]Email Address[/TD]
[TD]Remittance Date[/TD]
[TD]Remit File Name[/TD]
[TD]Standard Remit Naming[/TD]
[TD]File Path[/TD]
[TD]On ACH Run?[/TD]
[/TR]
[TR]
[TD]AFS Logistics [/TD]
[TD]Accounting[/TD]
[TD]ar@afs.net[/TD]
[TD]9/5/2017[/TD]
[TD] [/TD]
[TD]_952017_paymentadvice.pdf[/TD]
[TD]G:\Accts Payable\ACH Remittance\vendor ACH 09.05.17\_952017_paymentadvice.pdf[/TD]
[TD]No[/TD]
[/TR]
[TR]
[TD]Ahlstrom[/TD]
[TD]Derek[/TD]
[TD]derek.chatman@ahlstrom-munksjo.com[/TD]
[TD]9/5/2017[/TD]
[TD]AhlstromNonwovensLLC[/TD]
[TD]AhlstromNonwovensLLC_952017_paymentadvice.pdf[/TD]
[TD]G:\Accts Payable\ACH Remittance\vendor ACH 09.05.17\AhlstromNonwovensLLC_952017_paymentadvice.pdf[/TD]
[TD]Yes
[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
.
Here is a link to download the completed workbook :

https://www.amazon.com/clouddrive/share/fNgKf9iXiRVcCs9GzyBmhydggzXgVJhal4vjzfmUa9a

Paste your path/s where appropriate ( G:\Accts Payable\ACH Remittance\vendor ACH 09.05.17\_952017_paymentadvice.pdf & G:\Accts Payable\ACH Remittance\vendor ACH 09.05.17\AhlstromNonwovensLLC_952017_paymentadvice.pdf )

If it still throws an error, try changing the name of file without using the underscores " _ ".


I've experienced issues with file names using the underscore. If it still does not work, there must be a spelling error somewhere, or the file is not located where the path line says, or ???
As indicated previously, the project runs well here.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top