Sending Attachments via Outlook - VBA if attachment file does not exist

snuffnchess

Board Regular
Joined
May 15, 2015
Messages
71
Office Version
  1. 365
Platform
  1. Windows
I need to come up with a work around for IF an attachment is unable to be located that the script will move on to the next email and then place emdata.Range("A"&r) value on erdata.Range - Column C... and then just start from row 2 going down.

I was dumb when i created the error handling for if "sfile ="" because that just cannot happen in this code. So instead somehow need to make it so that if emAttach file does not exist....... How would this go about getting done?



VBA Code:
Option Explicit
Public sfolder As String
Public sfile As String


Sub SendMail()
    Dim objOutlook As Object
    Dim objMail As Object

    Dim emTo As String
    Dim emRep As String
    Dim emCC
    Dim emSubject As String
    Dim emBody As String
    Dim emAttach As String
   
    Dim emdata As Worksheet
    Dim ob As Workbook
   
    Dim r As Long
    Dim emrng As Range
   
   
    Set ob = ThisWorkbook
    Set emdata = ob.Sheets("Email")
   
   
   
    sfolder = emdata.Range("L1").Value

    Set objOutlook = CreateObject("Outlook.Application")

    For r = 2 To ActiveSheet.Range("A2").End(xlDown).Row
        With ActiveSheet
            Set emrng = emdata.Range(emdata.Cells(r, 5), emdata.Cells(r, 10))
            .Range("D" & r).Value = WorksheetFunction.TextJoin(";", True, emrng)
           
            emTo = .Range("D" & r)
                If emTo = "" Then GoTo nextline
            emCC = .Range("C" & r).Value
            emRep = .Range("C" & r).Value
            emSubject = .Range("A" & r).Value & " - DSO Report - " & Format(Now, "yyyy-mm-dd")
            sfile = emdata.Range("A" & r).Value & ".xlsx"
                If sfile = "" Then GoTo nextline
            emAttach = sfolder & "\" & sfile
            emBody = "<p>Hello <b>" & .Range("A" & r).Value & " team</b>,</p>" _
                    & "<p>Attached is this weeks DSO File with data from ::startdate:: to ::enddate::</p>" _
                    & "<p>Please let us know if you have any questions.</p>" _
                    & "<p>Team " & .Range("B" & r).Value & "<br>" & .Range("c" & r).Value & "</a></p>"
        End With

        Set objMail = objOutlook.CreateItem(0)
        With objMail
            .To = emTo
            .cc = emCC
            .ReplyRecipients.Add emRep
            .Subject = emSubject
            .HTMLBody = "<html><head></head><body>" & emBody & "</body></html>"
            .Attachments.Add emAttach
            .Display
            '.Send ' If you want to send it without clicking
        End With
nextline:
    Next
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Okay correction to the question... i was able to make the code skip over and move on to the next email if the attachment does not exist... but how do I make it so that it would put the value of ("A" & r) in column C of the erdata worksheet?
VBA Code:
Option Explicit
Public sfolder As String
Public sfile As String


Sub SendMail()
    Dim objOutlook As Object
    Dim objMail As Object

    Dim emTo As String
    Dim emRep As String
    Dim emCC
    Dim emSubject As String
    Dim emBody As String
    Dim emAttach As String
    
    Dim emdata As Worksheet
    Dim ob As Workbook
    
    Dim r As Long
    Dim emrng As Range
    
    
    Set ob = ThisWorkbook
    Set emdata = ob.Sheets("Email")
    
    
    
    sfolder = emdata.Range("L1").Value

    Set objOutlook = CreateObject("Outlook.Application")

    For r = 2 To ActiveSheet.Range("A2").End(xlDown).Row
        With ActiveSheet
            Set emrng = emdata.Range(emdata.Cells(r, 5), emdata.Cells(r, 10))
            .Range("D" & r).Value = WorksheetFunction.TextJoin("; ", True, emrng)
            
            emTo = .Range("D" & r)
                If emTo = "" Then GoTo nextline
            emCC = .Range("C" & r).Value
            emRep = .Range("C" & r).Value
            emSubject = .Range("A" & r).Value & " - DSO Report - " & Format(Now, "yyyy-mm-dd")
            sfile = emdata.Range("A" & r).Value & ".xlsx"
            emAttach = sfolder & "\" & sfile
            
            If Len(Dir(emAttach, vbDirectory)) = 0 Then GoTo nextline
            
            
            emBody = "<p>Hello <b>" & .Range("A" & r).Value & " team</b>,</p>" _
                    & "<p>Attached is this weeks DSO File with data from ::startdate:: to ::enddate::</p>" _
                    & "<p>Please let us know if you have any questions.</p>" _
                    & "<p>Team " & .Range("B" & r).Value & "<br>" & .Range("c" & r).Value & "</a></p>"
        End With

        Set objMail = objOutlook.CreateItem(0)
        With objMail
            .To = emTo
            .cc = emCC
            If emRep = "" Then GoTo skip
                .ReplyRecipients.Add emRep
skip:
            .Subject = emSubject
            .HTMLBody = "<html><head></head><body>" & emBody & "</body></html>"
            .Attachments.Add emAttach
            .Display
            '.Send ' If you want to send it without clicking
        End With
nextline:
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,537
Messages
6,179,405
Members
452,911
Latest member
a_barila

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