How to bypass VBA error message (1 Viewer)

miacino

Registered User.
Local time
Today, 06:28
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
---------------
 

NauticalGent

Ignore List Poster Boy
Local time
Today, 09:28
Joined
Apr 27, 2015
Messages
6,280
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
 

theDBguy

I’m here to help
Staff member
Local time
Today, 06:28
Joined
Oct 29, 2018
Messages
21,357
Hi. Just offering another approach...
Code:
With MailOutlook
    .To=...
    .Subject=...
    If Dir(FileName)<>"" Then
        .Attachments.Add FileName
    End If
...
 
Last edited:

NauticalGent

Ignore List Poster Boy
Local time
Today, 09:28
Joined
Apr 27, 2015
Messages
6,280
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!
 

theDBguy

I’m here to help
Staff member
Local time
Today, 06:28
Joined
Oct 29, 2018
Messages
21,357
Hi NG. Thanks. It's fixed. Cheers!
 

miacino

Registered User.
Local time
Today, 06:28
Joined
Jun 5, 2007
Messages
106
That did it DBGuy! Thank you both so much!
 

NauticalGent

Ignore List Poster Boy
Local time
Today, 09:28
Joined
Apr 27, 2015
Messages
6,280
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

Top Bottom