How to bypass VBA error message

miacino

Registered User.
Local time
Today, 14:18
Joined
Jun 5, 2007
Messages
106
I have a code which on pressing command button, it opens email and attaches a file from my computer.

If it does not find the file, I get a halt error, "cannot find this file". Is there a way to build into the code to continue to sending the email even if the file is not found/attached?
---------------
Private Sub Command581_Click()
Dim strFileName As String
Dim rst As DAO.Recordset

Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
FileName = "G:\CCSG Physicians\1 - Physicians - CV's & Interview Itineraries\CV's" & [Text579] & " CV.pdf"
With MailOutLook
.To = "janedoe@aol.com"
.Subject = "New Hire Offer Made"
.Attachments.Add FileName
.Display
End With

End Sub
---------------
 
You could simply put "On Error Resume Next" at the top of your sub. Not usually recommended.

Best to trap for that specific error and then handle it.

PBaldy's site has a good article with an example:
http://baldyweb.com/ErrorTrap.htm
 
Hi. Just offering another approach...
Code:
With MailOutlook
    .To=...
    .Subject=...
    If Dir(FileName)<>"" Then
        .Attachments.Add FileName
    End If
...
 
Last edited:
Hi. Just offering another apprpoach...

As opposed to an approach? ;)

A much better fix. I focused on the OP's question rather then best-practices. One of the reasons I will never rise above "Contender" status!
 
Hi NG. Thanks. It's fixed. Cheers!
 
Glad you have it sorted. I do highly encourage you to give the site a good read over even though it wasn't the best fix for your problem.

As you get deeper and deeper into VBA, error handling is a MUST.
 

Users who are viewing this thread

Back
Top Bottom