I would like to use excel to send an email to the email address in column C from C5-C42 when the corresponding cell in column F contains the text "expired". I've been at this for over four days. I appreciate any help I can get.
I also keep getting a run-time error 424.
Below is my code:
Private Sub CommandButton1_Click()
If Target.Cells.Count > 1 Then Exit Sub
If Not Application.Intersect(Range("F5:F42"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value = "Expired" Then
Call Mail_small_Text_Outlook
End If
End If
End Sub
Sub Mail_small_Text_Outlook()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "Hi there" & vbNewLine & vbNewLine & _
"Your certification has expired." & vbNewLine & _
"Please contact an admin."
On Error Resume Next
With OutMail
.To = "emailaddress#net.com"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = strbody
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Try this:
Private Sub CommandButton1_Click()
Dim c As Range
For Each c In Range("F5:F42")
If c.Value2 = "Expired" Then Call Mail_small_Text_Outlook(c.Offset(0, -3).Value2)
Next c
End Sub
This first routine is triggered by the command button click. It cycles through each cell in range F5:F42. If the cell has "Expired" as a value, it calls the mail routine, and passes to it the value contained in column C (by using the F column address -3 columns)
Sub Mail_small_Text_Outlook(emailAddress As String)
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "Hi there" & vbNewLine & vbNewLine & _
"Your certification has expired." & vbNewLine & _
"Please contact an admin."
On Error Resume Next
With OutMail
.To = emailAddress
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = strbody
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
The mail routine accepts an email address as a parameter and hopefully you'll notice it replaces the generic "email address.com" you had on the .To line
Please be aware that currently the code is creating a new instance of Outlook for every time it needs to send an e-mail, without closing it. I think you can quit Outlook simply with the line OutApp.Quit so try sticking that in at the end of the Mail_small_Text_Outlook routine
Related
I have a sheet which has an inventory of CDs. If one CD inventory goes under 10 pieces then the sheet triggers mail which is sent automatically.
When I press enter somewhere in the sheet it sends mail repetitively.
Sub Mail_Radio_Waldrand()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "Hallo Sari" & vbNewLine & vbNewLine & _
"Der CD-Bestand von Radio Waldrand ist unter dem Mindestbestand von 10 Stück." & vbNewLine & _
"Bitte bestellen."
On Error Resume Next
With OutMail
.To = "opr6#dreischiibe.ch"
.CC = ""
.BCC = ""
.Subject = "Marius & die Jagdkapelle: CD-Bestand von Radio Waldrand unterschritten"
.Body = strbody
'You can add a file like this
'.Attachments.Add ("C:\test.txt")
'or use .Send
.Send
End With
On Error Resume Next
With OutMail
.To = "opr6#dreischiibe.ch"
.CC = ""
.BCC = ""
.Subject = "Marius & die Jagdkapelle: CD-Bestand von Radio Waldrand unterschritten"
.Body = strbody
'You can add a file like this
'.Attachments.Add ("C:\test.txt")
'or use .Send
.Send
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
I tried If OutMail = True Then Exit Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Call Materialbestand
End Sub
Sub Materialbestand()
If Worksheets("2023 Materialbestand").Range("B9") < 20 Then
Call Mail_Luftpolster_klein
End If
End Sub
Focus WorkSheet_Change on a single cell.
Private Sub WorkSheet_Change(ByVal Target As Range)
If Target.Address = "$B$9" Then
Call Materialbestand
End If
End Sub
I'm trying to automate a report by being able to send a selected range of cells, that includes hidden cells, by making a macro. When I try to run the macro, it is blank. Please note that the text not written as a code are instructions. Macro is shown below:
Sub SendforApproval()
'
' SendforApproval Macro
'
' Generate Email
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim rng As Range
If ActiveWorkbook.Path <> "" Then
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Set myOlApp = CreateObject("Outlook.Application")
'This is where the body of the email is populated - you can point to cells in your worksheet to pull text from them
strbody = "<font size=""3"" face=""Cambria"">" & _
"Hi " & Range("B5") & ",<br>" & _
"<br>Please note finance request #" & Range("a7") & " has been accepted. Upon review, please use voting buttons to Approve or Send for Rework.<br>"
' The range belows designates which portion of your sheet will be inserted in the body of the email
Set rng = Sheets("SheetName").Range("A7:T22").SpecialCells(xlCellTypeVisible)
'You can also use a fixed range if you want or flex range
'Set rng = Sheets("YourSheet").Range("D4:D12").SpecialCells(xlCellTypeVisible)
'Set rng = Range("f7").Resize(Application.CountA(Range("f7:f" & Rows.Count)), 12)
On Error GoTo 0
If rng Is Nothing Then
MsgBox "The selection is not a range or the sheet is protected" & _
vbNewLine & "please correct and try again.", vbOKOnly
Exit Sub
End If
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
' This section defines receipants, Subject
On Error Resume Next
With OutMail
.To = Range("D7")
.CC = "yourname#email.com"
.BCC = ""
.Subject = "Finance Request #" & Range("a7")
' This section adds Voting buttons
.Display 'or use .Send to automatically send without giving opportunity to review final product
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
' End of email process
End If
End Sub
you should use "on error resume next" with more caution. You dont see the exception that occurs.
your failure is that you want to get the value of the cells D7 and A7 (string) but you take instead the range (object).
Try the following:
With OutMail
.To = Range("D7").Value
.CC = "yourname#email.com"
.BCC = ""
.Subject = "Finance Request #" & Range("A7").Value
' This section adds Voting buttons
.Display 'or use .Send to automatically send without giving opportunity to review final product
End With
Is there a more efficient way to send email reminders based on a cell value that changes frequently?
'This is the main function
Sub notify()
Dim rng As Range
For Each rng In Range("F3:F14")
If (rng.Value = 1) Then
Call mymacro
End If
Next rng
End Sub
'-----------------------------------------------------------------------
'This is the function that sends an email when called by the main function
Private Sub mymacro()
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xMailBody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2"
On Error Resume Next
With xOutMail
.To = "email address"
.CC = ""
.BCC = ""
.Subject = "test succeeded"
.Body = xMailBody
.Display 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
Both codes are in the same module of my worksheet. The code sends an email (through Outlook) to the user. For example, if F3 and F7 evaluate to true, two emails will be sent to the user.
How can I, if the same situation occurs (F3 and F7 evaluate to true), the two email sent to the user would specify which cell evaluated to true. In other words, each email sent would be different in pointing out which specific cell evaluated to true.
Also, would the code be able to rerun if the data inside the cell ("F3:F14") is updated?
On refresh of query, the code should check each cell from F3 to F14 and see if it is equal to 1, if so, it will email user the cell location.
UPDATE:
'Need to be in the sheet code:
Private Sub Worksheet_Change(ByVal Target As Range)
Call notify
End Sub
Sub notify()
Dim rng As Range
For Each rng In Range("F3:F14")
If (rng.Value = 1) Then
Call mymacro(rng.Address)
End If
Next rng
End Sub
Private Sub mymacro(theValue As String)
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xMailBody = "Hi there" & vbNewLine & vbNewLine & _
"The value that changed is in cell: " & theValue
On Error Resume Next
With xOutMail
.To = "email address"
.CC = ""
.BCC = ""
.Subject = "test succeeded"
.Body = xMailBody
.Display 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
Could you please help me to automatically send an email from Excel only when the formula value in column M (=IF(VAL.EMPTY(K15);"";MAX(K15-Today();0))>200. Unfortunately the Sheet1 code triggers the email code if the condition is met (>200) in formula value cell in column M if the date in column K is altered manually or by writing manually Not Sent in column N. Instead my goal would be:
1) to understand why this code in sheet1 doesn't send the email automatically as supposed to do (the only thing it does is to put Sent in column N without sending the email. This make me think that this code works)
2) to find the way to send the email automatically without changing anything manually in the cells in my sheet1.
H I J K L M N
Date Score Description Next Due Status Days till
expiration
15 28/09/2017 13 Medium Risk 25/07/2018 Valid 284 Sent
16 11/10/2017 13 Medium Risk 10/08/2018 Valid 300 Sent
'Sheet1 (FormulaValueChange)
Private Sub Worksheet_Calculate()
Dim FormulaRange As Range
Dim NotSentMsg As String
Dim MyMsg As String
Dim SentMsg As String
Dim MyLimit As Double
NotSentMsg = "Not Sent"
SentMsg = "Sent"
'Above the MyLimit value it will run the macro
MyLimit = 200
'Set the range with the Formula that you want to check
Set FormulaRange = Me.Range("M15:M16")
On Error GoTo EndMacro:
For Each FormulaCell In FormulaRange.Cells
With FormulaCell
If IsNumeric(.Value) = False Then
MyMsg = "Not numeric"
Else
If .Value > MyLimit Then
MyMsg = SentMsg
If .Offset(0, 1).Value = NotSentMsg Then
Call Mail_with_outlook1(FormulaCell)
End If
Else
MyMsg = NotSentMsg
End If
End If
Application.EnableEvents = False
.Offset(0, 1).Value = MyMsg
Application.EnableEvents = True
End With
Next FormulaCell
ExitMacro:
Exit Sub
EndMacro:
Application.EnableEvents = True
MsgBox "Some Error occurred." _
& vbLf & Err.Number _
& vbLf & Err.Description
End Sub
'Mail Code
Option Explicit
Public FormulaCell As Range
Sub Mail_with_outlook1(FormulaCell As Range)
Dim OutApp As Object
Dim OutMail As Object
Dim strto As String, strcc As String, strbcc As String
Dim strsub As String, strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strto = "tom#something.abc"
strcc = ""
strbcc = ""
strsub = "Assessement reminders"
strbody = "Thanks a lot"
With OutMail
.To = strto
.CC = strcc
.BCC = strbcc
.Subject = strsub
.Body = strbody
'You can add a file to the mail like this
'.Attachments.Add ("C:\test.txt")
.Display ' or use .Send
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
You can do it this way.
Sub Mail_small_Text_Outlook()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Working in Excel 2000-2016
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "Hi there" & vbNewLine & vbNewLine & _
"Cell A1 is changed" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"
On Error Resume Next
With OutMail
.To = "ron#debruin.nl"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = strbody
'You can add a file like this
'.Attachments.Add ("C:\test.txt")
.Display 'or use .Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
https://www.rondebruin.nl/win/s1/outlook/bmail9.htm
In excel I have the following code which sends out emails for every cell that contains an email address in column K.
This would work except for the header in the table isn't an email address, so it breaks the code. I tried to skip the header by specifying "if cell.value = CONTACT METHOD, which is the header name text, then go to Next cell"
but this causes a "Next without for" error.
Sub Mail_small_Text_Outlook()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Working in Office 2000-2013
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
On Error GoTo cleanup
For Each cell In Columns("K").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value Like "*#*" Then
finaladdress = cell.Value
Else
finaladdress = cell.Value & "#email.smsglobal.com"
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = finaladdress
.Subject = "Reminder"
.Body = "Dear " & Cells(cell.Row, "A").Value _
& vbNewLine & vbNewLine & _
"Please contact us to discuss bringing " & _
"your account up to date"
'You can add files also like this
'.Attachments.Add ("C:\test.txt")
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
End If
Next cell
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub
If your goal is to skip cell K1 in looping down column K then:
For Each cell In Columns("K2:K" & Rows.Count).Cells.SpecialCells(xlCellTypeConstants)
You can enclose the code within the FOR/EACH loop within a separate IF statement, as below:
Sub Mail_small_Text_Outlook()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Working in Office 2000-2013
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
On Error GoTo cleanup
For Each cell In Columns("K").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value = "CONTACT METHOD" Then
'Do Nothing, or Enter code here
Else
If cell.Value Like "*#*" Then
finaladdress = cell.Value
Else
finaladdress = cell.Value & "#email.smsglobal.com"
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = finaladdress
.Subject = "Reminder"
.Body = "Dear " & Cells(cell.Row, "A").Value _
& vbNewLine & vbNewLine & _
"Please contact us to discuss bringing " & _
"your account up to date"
'You can add files also like this
'.Attachments.Add ("C:\test.txt")
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
End If
End If
Next cell
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub