View Full Version : Send Object


thingssocomplex
03-31-2009, 12:28 PM
Hi Guys,

Hope you are all well

I've only recently started using VBA and learning from books, however I can't find the answer I need. I created a SendObject command in VBA which works fine when user clicks on the button the email appears and if email is sent everything is great. When I was testing and clicked on the button I did not want to send the email, so I closed the email window resulting in a error message

"Run-time error '2501'" what do I need to add to my code so that if this occurs the run-time error will not appear asking to debug, this would very much confuse users, my code is below

DoCmd.SendObject acSendQuery, "Contact_Name", acFormatXLS, "Jon Doe", , , "Test Subject"

boblarson
03-31-2009, 12:37 PM
Hi Guys,

Hope you are all well

I've only recently started using VBA and learning from books, however I can't find the answer I need. I created a SendObject command in VBA which works fine when user clicks on the button the email appears and if email is sent everything is great. When I was testing and clicked on the button I did not want to send the email, so I closed the email window resulting in a error message

"Run-time error '2501'" what do I need to add to my code so that if this occurs the run-time error will not appear asking to debug, this would very much confuse users, my code is below

DoCmd.SendObject acSendQuery, "Contact_Name", acFormatXLS, "Jon Doe", , , "Test Subject"

Add an error handler:


On Error GoTo err_handler

DoCmd.SendObject acSendQuery, "Contact_Name", acFormatXLS, "Jon Doe", , , "Test Subject"

exit_proc:
Exit Sub

err_handler:
If Err.Number <> 2501 Then
MsgBox Err.Description, vbExclamation, "Error Number: " & Err.Number
Resume exit_proc
End If
End Sub

thingssocomplex
03-31-2009, 12:47 PM
Wow Mr.Speedy thanks for the reply this worked a treat, can I use something very similar to handle other errors I may get, I then don't have to keep nagging at you guys?

boblarson
03-31-2009, 12:54 PM
Wow Mr.Speedy thanks for the reply this worked a treat, can I use something very similar to handle other errors I may get, I then don't have to keep nagging at you guys?

Yep, you sure can. I would use a select case statement if you do checking for more than a couple specific error numbers though.

thingssocomplex
03-31-2009, 01:00 PM
Cheers Bob, Just one more query/question if I may last one I promise, when writing text for the email, after entering subject how do I make it drop down a line so when the user clicks the button to generate email it would read something like

Hi Bob,

Please find the attached

Instean of reading

Hi Bob, Please find the attached

boblarson
03-31-2009, 01:04 PM
Add a

& Chr(13) & Chr(10) &

between the text you want on another line. If you want to skip two lines then use:

& Chr(13) & Chr(10) & Chr(13) & Chr(10) &

thingssocomplex
03-31-2009, 01:16 PM
Hi Bob,

Thanks for you help, I will keep learning difficult from text books at times, not real life situation.

Once again many thanks

boblarson
03-31-2009, 01:18 PM
By the way, the CHR method will work in control sources, in queries, in macros, and in VBA Code. In VBA, however, you have another way you can do it. You can use the constant vbCrLf which will do the same as a Chr(13) & Chr(10) together.