Error Handling, Loop, External Paths

Orbik

Registered User.
Local time
Today, 02:16
Joined
Mar 25, 2013
Messages
13
Hi All

I am setting up a form to send multiple attachments to an email.
I have a query "qry_DwgEmail" that has an ID and a path column. On the form I have a button with the code below in the Click Event.

The code below all works fine unless the path is incorrect, so I would like some error handling to be able to either:
1) Stop the code from running and display a message box with my message.
2) Continue the loop and then have the message box display what files could not be found (Preferred).

Note, the following code was taken from one of the forums I am not 100% on what it all means.



Code:
Private Sub btn_CreateEmail_Click()
Dim MyDB As Database
Dim MyRS As Recordset
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookAttach As Outlook.Attachment
Dim TheAttachment As String

Dim QDF As QueryDef
Dim prm As Parameter

Set objOutlook = CreateObject("Outlook.Application")
Set objOutlookMsg = objOutlook.CreateItem(0) 'olMailItem

Set MyDB = CurrentDb
Set QDF = MyDB.QueryDefs("qry_DwgEmail")
For Each prm In QDF.Parameters
prm.Value = Eval(prm.Name)
Next prm
Set MyRS = QDF.OpenRecordset
MyRS.MoveFirst

On Error GoTo Handler:
With objOutlookMsg
Do Until MyRS.EOF
TheAttachment = MyRS![directoryFullPath]
Set objOutlookAttach = .Attachments.Add(TheAttachment)
MyRS.MoveNext
Loop
Handler:
MsgBox "Not All Of The Selected Files Have Been Found" & vbCrLf & vbCrLf & "Double Click Drawing Number And ReLink File"

'.To = "myeamil@yahoo.com"
.Subject = "Profile Lighting Drawings"
.Body = "Please find attached drawings " & vbCrLf & vbCrLf & "Kind Regards"
'.Send

End With

objOutlookMsg.Display
Email_Err_Clear:


End Sub

Thanks
 
I think job 1 would be to learn what it all means. It's great to get code from forums or whatever, but you should understand it when incorporating it into your application.

To your question, you can use

If Len(Dir(MyRS![directoryFullPath])) > 0 Then

which would mean the path existed.
 
Hi pbaldy, Sorry I lost this post and couldn't figure out how to recall it in my preferences.
Thanks for the reply, I have sorted it out and learnt what everything in the code meant as per your instruction.
Thanks again.
 

Users who are viewing this thread

Back
Top Bottom