email only when a certain cell is populated

rooster05

New Member
Joined
Mar 4, 2017
Messages
34
i have code to email when an address (several in dropdown list) which is housed in C51, but what i need is for this to only be active or be able to be sent when C50 is populated or at least contain certain text.

would this be possible to do

many thanks

Private Sub CommandButton1_Click()Dim OutApp As Object
Dim OutMail As Object
Dim mess As Object, recip As String
recip = [c51].Value
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.Recipients.Add recip
.CC = ""
.BCC = ""
.Subject = ""
.Body = ""
.Attachments.Add ActiveWorkbook.FullName
.display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Should be able to achieve by adding proper code to the sheet level of your workbook.

Code:
Option Explicit


Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range


Set rng = Intersect(Target, [C51])
If Not rng Is Nothing Then
    Dim OutMail As Object
    Dim mess As Object, recip As String
    
    recip = [C51].Value
    If recip.Value Like "?*@?*.?*" Then
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
        On Error Resume Next
        With OutMail
            .Recipients.Add recip
            .CC = ""
            .BCC = ""
            .Subject = ""
            .Body = ""
            .Attachments.Add ActiveWorkbook.FullName
            .display
        End With
        On Error GoTo 0
        Set OutMail = Nothing
        Set OutApp = Nothing
    End If
End If
End Sub
 
Upvote 0
Many thanks for this, however when i use this code i get a compile error: invalid qualifier, i kept the code as is and i have also substituted the ?*@?*.?*please select. could you advise what i'm doing wrong or missing

many thanks
 
Upvote 0
With Option Explicit, all variables must be declared before using them. I over looked it too, but we are missing declaration of OutApp.

Code:
    Dim OutMail As Object
[COLOR=#ff0000]    Dim OutApp As Object  [/COLOR][COLOR=#008000]'This was missing from code for post #2[/COLOR][COLOR=#ff0000][/COLOR]
    Dim mess As Object, recip As String
 
Upvote 0
thank you for the speedy quick reply, again i'm having issues with the line " If recip.Value Like "?*@?*.?*" Then "- recip is highlighted blue, is there something i'm doing wrong
 
Upvote 0
I think recip is throwing things off... Try replacing the whole thing with this:
Code:
Option Explicit


Private Sub Worksheet_Change(ByVal Target As Range)
'Working in Excel 2000-2016
'This example send the last saved version of the Activeworkbook
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm


Dim OutApp As Object
Dim OutMail As Object
Dim rng As Range
Dim Whom$


Set rng = Intersect(Target, [C51])
If Not rng Is Nothing Then
    Whom = [C51]
    If Not Whom Like "?*@?*.?*" Then Exit Sub
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)


    On Error Resume Next
    With OutMail
        .to = Whom
        .CC = ""
        .BCC = ""
        .Subject = ""
        .Body = ""
        .Attachments.Add ActiveWorkbook.FullName
        .Display    'or use .Send
    End With
    On Error GoTo 0


    Set OutMail = Nothing
    Set OutApp = Nothing
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,996
Members
452,373
Latest member
TimReeks

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