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

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
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
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:
G:\Accts Payable\ACH Remittance\vendor ACH 09.05.17\_952017_paymentadvice.pdf

<tbody>
</tbody>
 
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
A
B
C
D
E
F
G
H
1
LaptopMr. Meme@yahoo.comAsus 101C:\Users\My\Desktop\Laptops.txtyes
2
BooksMr. Youyou@yahoo.comDell BDAC:\Users\My\Desktop\Book1.xlsxyes
3
4
5

<tbody>
</tbody>
Sheet: Sheet1

<tbody>
</tbody>
 
Last edited:
Upvote 0
Vendor NameAddress ToEmail AddressRemittance DateRemit File NameStandard Remit NamingFile PathOn ACH Run?
AFS Logistics Accountingar@afs.net9/5/2017 _952017_paymentadvice.pdfG:\Accts Payable\ACH Remittance\vendor ACH 09.05.17\_952017_paymentadvice.pdfNo
AhlstromDerekderek.chatman@ahlstrom-munksjo.com9/5/2017AhlstromNonwovensLLCAhlstromNonwovensLLC_952017_paymentadvice.pdfG:\Accts Payable\ACH Remittance\vendor ACH 09.05.17\AhlstromNonwovensLLC_952017_paymentadvice.pdfYes

<colgroup><col><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>
 
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,214,553
Messages
6,120,176
Members
448,948
Latest member
spamiki

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