Prevent e-mail if condition not met

Niven

New Member
Joined
Aug 23, 2011
Messages
45
Hi, What is wrong with this code?Sub Send_via_email(Cancel As Boolean)'' Send_via_email Macro' Dim blnCancel As Boolean Dim sMail As String Dim rngCell1 As Excel.Range, rngCell2 As Excel.Range sMail = Application.Dialogs(xlDialogSendMail).Show blnCancel = Not Sheets("Weekly Timesheet").Range("C44").Value If blnCancel Then Call MsgBox(Prompt:="Have ALL hours worked been allocated correctly?", Title:="Cannot send e-mail", Buttons:=vbExclamation + vbOKOnly)' If Cancel = blnCancel Then sMail End IfEnd Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi Niven

For one it's fairly illegible :LOL:

This is what you have posted, but broken down line-by-line and indented:
Code:
Sub Send_via_email(Cancel As Boolean)
    '' Send_via_email Macro'
    Dim blnCancel As Boolean
    Dim sMail As String
    Dim rngCell1 As Excel.Range, rngCell2 As Excel.Range
    
    sMail = Application.Dialogs(xlDialogSendMail).Show
    blnCancel = Not Sheets("Weekly Timesheet").Range("C44").Value
    
    If blnCancel Then
        Call MsgBox(Prompt:="Have ALL hours worked been allocated correctly?", Title:="Cannot send e-mail", Buttons:=vbExclamation + vbOKOnly)
        'If Cancel = blnCancel Then sMail
    End If
End Sub

You haven't said what the problem is but I'm guessing it's allowing you to send email regardless of whether or not blnCancel is True/False.
The reason for that is you are calling the SendMail dialog BEFORE you test whether or not it should be cancelled. That bit should come after... Also I don't see the point in having a Cancel argument for the routine

Code:
Sub Send_via_email()
    Dim blnCancel As Boolean
    Dim sMail As String

    blnCancel = Not Sheets("Weekly Timesheet").Range("C44").Value
    If blnCancel Then
        Call MsgBox(Prompt:="Have ALL hours worked been allocated correctly?", Title:="Cannot send e-mail", Buttons:=vbExclamation + vbOKOnly)
        Exit Sub
    Else
        Application.Dialogs(xlDialogSendMail).Show
    End If
End Sub

Finally; consider a different approcah for sending mail. There are plenty of ways shown here: Ron De Bruin - Send Mail. This way your code can actually send the mail rather than just create a mail item.

Edit: To paste code in the forum do the following:
1) Copy the code from the VBE
2) In the forum post body type [code]; then past your code; and at the end of the code type [/code]
 
Last edited:
Upvote 0
Thanks a lot you're a genius! I tried copying and pasting but the code still came up all in one paragraph. The code works perfectly! I don't quite understand the other option you spoke about i.e. sending the mail without opening outlook.
 
Upvote 0
Hi I am trying to post a code but when I preview the post it still comes up with all the code lumped together. I typed
Code:
 before the first word and then at the end I typed
.
 
Upvote 0
I don't know why but I am trying to post a code but it doesn't appear correctly and I am using what you told me - to type code in brackets. The entire code appears in one long paragraph.
 
Last edited:
Upvote 0
Hi Sorry I tried it a few times, copied from the Word doc and pasted and then when I preview post it is all in a mess???
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

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