how to incorporate error handling into this

bdouglas1011

New Member
Joined
Jul 28, 2014
Messages
38
I have this code
VBA Code:
With olEmail
        .Display
        .HTMLBody = EmailBody & .HTMLBody
        .To = GetSurveyEmailDistList
        .CC = GetSurveyEmailCCList & "[EMAIL]Remote@legacydirectional.com[/EMAIL]"
                .Subject = ESubject
       
    'Attach files. The var "i" should start at the first row of file names, and go to the last row
                                 
        For i = row + 2 To row + 15
        If cells(i, col).Value = "Yes" Then
        .Attachments.Add (Directory & cells(i, col + 2).Value)
        End If
        Next i

    End With

Sometimes it looks for a file that did not get generated but I would like it to continue and just give a message to correct it for next time.
It is a runtime error saying cant find file verify path and name are correct.

Is this possible
 
Okay, so this will display a message when the cell is blank or when the file doesn't exist . . .

VBA Code:
        Dim currentFilename As String

        For i = Row + 2 To Row + 15
            If Cells(i, col).Value = "Yes" Then
                If Len(Cells(i, col + 2)) > 0 Then
                    currentFilename = Directory & Cells(i, col + 2).Value
                    If Len(Dir(currentFilename, vbNormal)) > 0 Then
                        .Attachments.Add currentFilename
                    Else
                        MsgBox "'" & currentFilename & "' does not exist!", vbExclamation
                    End If
                Else
                    MsgBox "Filename not found!", vbExclamation
                End If
            End If
        Next i
 
Upvote 0
Solution

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Okay, so this will display a message when the cell is blank or when the file doesn't exist . . .

VBA Code:
        Dim currentFilename As String

        For i = Row + 2 To Row + 15
            If Cells(i, col).Value = "Yes" Then
                If Len(Cells(i, col + 2)) > 0 Then
                    currentFilename = Directory & Cells(i, col + 2).Value
                    If Len(Dir(currentFilename, vbNormal)) > 0 Then
                        .Attachments.Add currentFilename
                    Else
                        MsgBox "'" & currentFilename & "' does not exist!", vbExclamation
                    End If
                Else
                    MsgBox "Filename not found!", vbExclamation
                End If
            End If
        Next i
perfect thank you
 
Upvote 0
Note when marking posts as a "Solution", you want to mark the actual post that originally contains the solution (not you own post acknowledging to someone that their post works).
You would only mark you own post if you came up with the working solution and posted it here.

I have updated it for you.
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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