Below is the code, but I also want a test that prevents me from sending an email if Cells B20 and B21 are empty.
Sub Send_Range_Or_Whole_Worksheet_with_MailEnvelope()
'Working in Excel 2002-2016
Dim AWorksheet As Worksheet
Dim Sendrng As Range
Dim rng As Range
On Error GoTo StopMacro
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Fill in the Worksheet/range you want to mail
'Note: if you use one cell it will send the whole worksheet
Set Sendrng = Worksheets("Sheet1").Range("A1:B21")
'Remember the activesheet
Set AWorksheet = ActiveSheet
With Sendrng
' Select the worksheet with the range you want to send
.Parent.Select
'Remember the ActiveCell on that worksheet
Set rng = ActiveCell
'Select the range you want to mail
.Select
' Create the mail and send it
ActiveWorkbook.EnvelopeVisible = True
With .Parent.MailEnvelope
' Set the optional introduction field thats adds
' some header text to the email body.
.Introduction = "Please see Quality Review Sev 1 details below."
With .Item
.To = "danielle.a.ext#razer.com"
.CC = ""
.BCC = ""
.Subject = Range("E2").Value
.send
End With
End With
MsgBox "Email Sent"
'select the original ActiveCell
rng.Select
End With
'Activate the sheet that was active before you run the macro
AWorksheet.Select
StopMacro:
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
ActiveWorkbook.EnvelopeVisible = False
End Sub
Test for the condition and use Exit Sub to end the sub routine.
If Len(Range("B20").Value) = 0 Or Len(Range("B21").Value) = 0 Then Exit Sub
Related
I would like to generate an email using a macro that I don't have to copy and paste the resulting data in the body of the email and attach the file. The worksheet pulls data from other worksheets and I use Excel functions to generate the syntax. So far the code I've used will only generate the email, subject line, and attachment but no body text. I am unable to show a photo or show the code.
Sub Send_Mail()
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Dim FileCell As Range
Dim rng As Range
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set rng = Nothing
On Error Resume Next
Set rng = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeVisible)
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
End With
For Each cell In rng
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = cell.Value
.Subject = "test mail"
The following code allows me to select a range of cells using in a range of a excel sheet and send that as an email. However, pictures or images withing the range are not selected. How do i do that
Sub Mail_Selection_Range_Outlook_Body()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Don't forget to copy the function RangetoHTML in the module.
'Working in Excel 2000-2016
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
'Set rng = Nothing
' On Error Resume Next
'Only the visible cells in the selection
'Set rng = Selection.SpecialCells(xlCellTypeVisible)
'You can also use a fixed range if you want
Set rng = Sheets("Sheet1").Range("B8:M108").SpecialCells(xlCellTypeVisible)
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)
On Error Resume Next
With OutMail
.To = Worksheets("Email").Range("A10").Value
.CC = Worksheets("Email").Range("B10").Value
.BCC = ""
.Subject = "This is the Subject line"
.HTMLBody = RangetoHTML(rng)
.Send 'or use .Display
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
I'm working on the Macro below which transfers the selected rows to a new sheet and deletes it from the original sheet when pressing a command button.
I'm trying to get it to send an automatic email when running this macro to inform xDepartment that yDepartment has transferred work. I want the body of the email to contain the active rows in their entirety which are being transferred.
At the moment, when tranferring the rows, I can click on any cell in the row in the yDepartment worksheet (adjacent and non-adjacent) and it will transfer columns A:L to the xDepartment worksheet. But when I add the macro to also send an email, it will only send the details of the specific cells that I have selected, instead of the full row.
Also, if the cells are non-adjacent (e.g. I'm transferring rows 4-5 and 8-10 at the same time), it sends the entire sheet, which I don't want.
Does anyone know how I can fix this so that when work is transferred, the automatic email contains the same content as that which is transferred?
Thanks in advance!
Sub Pass_to_xDepartment()
If MsgBox("Do you want to pass the selected tours to XDepartment?", vbYesNo, "Pass to XDepartment") = vbNo Then Exit Sub
For Each WSheet In ActiveWorkbook.Worksheets
If WSheet.AutoFilterMode Then
If WSheet.FilterMode Then
WSheet.ShowAllData
End If
End If
For Each DTable In WSheet.ListObjects
If DTable.ShowAutoFilter Then
DTable.Range.AutoFilter
DTable.Range.AutoFilter
End If
Next DTable
Next WSheet
Dim Sendrng As Range
On Error GoTo StopMacro
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Note: if the selection is one cell it will send the whole worksheet
Set Sendrng = Selection
'Create the mail and send it
With Sendrng
ActiveWorkbook.EnvelopeVisible = True
With .Parent.MailEnvelope
' Set the optional introduction field thats adds
' some header text to the email body.
.Introduction = "The following rows have been completed. "
With .Item
.To = "EMAIL"
.CC =
.BCC = ""
.Subject = "Updated"
.Send
End With
End With
End With
StopMacro:
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
ActiveWorkbook.EnvelopeVisible = False
'Declare variables
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Dim lastRow As Long
'Set variables
Set sht1 = Sheets("YDepartment")
Set sht2 = Sheets("XDepartment")
'Select Entire Row.Resize(ColumnSize:=12)
Intersect(Selection.EntireRow, Selection.Parent.Range("A:L")).Select
'Move row to destination sheet & Delete source row
lastRow = sht2.Range("A" & sht2.Rows.Count).End(xlUp).Row
With Selection
.Copy Destination:=sht2.Range("A" & lastRow + 1)
.EntireRow.Delete
End With
End Sub
In order to send the details of the whole row, you can set the Sendrng variable by choosing its contents via range rather than assigning it to selection.
Note: the code was slightly rearranged to send the email after transferring the data for this to work.
This should also compensate for selecting different ranges and wanting the avoid sending the entire sheet.
Sub Pass_to_xDepartment()
'Declare variables
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Dim lastRow As Long
Dim lastRow2 As Long
Dim WSheet As Variant
Dim DTable As Variant
Dim Sendrng As Range
Dim sht3 As Worksheet
If MsgBox("Do you want to pass the selected tours to XDepartment?", vbYesNo, "Pass to XDepartment") = vbNo Then Exit Sub
For Each WSheet In ActiveWorkbook.Worksheets
If WSheet.AutoFilterMode Then
If WSheet.FilterMode Then
WSheet.ShowAllData
End If
End If
For Each DTable In WSheet.ListObjects
If DTable.ShowAutoFilter Then
DTable.Range.AutoFilter
DTable.Range.AutoFilter
End If
Next DTable
Next WSheet
'Set variables
Set sht1 = Sheets("YDepartment")
Set sht2 = Sheets("XDepartment")
'Move row to destination sheet & Delete source row
lastRow = sht2.Range("A" & sht2.Rows.Count).End(xlUp).Row
'Select Entire Row.Resize(ColumnSize:=12)
Intersect(Selection.EntireRow, Selection.Parent.Range("A:L")).Select
With Selection
.Copy Destination:=sht2.Range("A" & lastRow + 1)
lastRow2 = sht2.Range("A" & sht2.Rows.Count).End(xlUp).Row
.EntireRow.Delete
End With
Set sht3 = ActiveWorkbook.Sheets.Add(After:=Worksheets(Worksheets.Count))
sht3.Name = "temp"
'Note: if the selection is one cell it will send the whole worksheet
Set Sendrng = sht2.Range("A" & (lastRow + 1) & ":L" & lastRow2)
Sendrng.Copy Destination:=sht3.Range("A1")
On Error GoTo StopMacro
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Create the mail and send it
sht3.Activate
lastRow2 = sht3.Range("A" & sht3.Rows.Count).End(xlUp).Row
Set Sendrng = sht3.Range("A1:L" & lastRow2)
With Sendrng
ActiveWorkbook.EnvelopeVisible = True
With .Parent.MailEnvelope
' Set the optional introduction field thats adds
' some header text to the email body.
.Introduction = "The following rows have been completed. "
With .Item
.To = "EMAIL"
.CC = ""
.BCC = ""
.Subject = "Updated"
.Send
End With
End With
End With
StopMacro:
Application.DisplayAlerts = False
ActiveWorkbook.Sheets("temp").Delete
Application.DisplayAlerts = True
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
ActiveWorkbook.EnvelopeVisible = False
End Sub
Also, its good practice to declare or Dim all your variables at the top of your programs but not necessary
Best of luck
I have a worksheet ("WC Referral Notice") that I need to put into the body of an email, but NOT send it immediately since I need to browse and attach several documents to the email before it is sent. I found macros to send it in the body of the email but it's sent as soon as you click. And I've also found macros to attach the workbook as an attachment. Neither of those are exactly what I'm looking for.
Sub Send_Selection_Or_ActiveSheet_with_MailEnvelope()
Dim Sendrng As Range
On Error GoTo StopMacro
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Sendrng = Selection
With Sendrng
ActiveWorkbook.EnvelopeVisible = True
With .Parent.MailEnvelope
.Introduction = " "
With .Item
.To = "adicker#generic.com"
.CC = ""
.BCC = ""
.Subject = "WC Referral Notice"
.Send
End With
End With
End With
StopMacro:
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
ActiveWorkbook.EnvelopeVisible = False
End Sub
Sub Send_Range_Or_Whole_Worksheet_with_MailEnvelope()
Dim AWorksheet As Worksheet
Dim Sendrng As Range
Dim rng As Range
On Error GoTo StopMacro
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Fill in the Worksheet/range you want to mail
'Note: if you use one cell it will send the whole worksheet
Set Sendrng = Worksheets("WC Referral Notice").Range("A1:H101")
'Remember the activesheet
Set AWorksheet = ActiveSheet
With Sendrng
' Select the worksheet with the range you want to send
.Parent.Select
'Remember the ActiveCell on that worksheet
Set rng = ActiveCell
'Select the range you want to mail
.Select
' Create the mail and send it
ActiveWorkbook.EnvelopeVisible = True
With .Parent.MailEnvelope
' Set the optional introduction field thats adds
' some header text to the email body.
.Introduction = "This is test mail 2."
With .Item
.To = "adicker#generic.com"
.CC = ""
.BCC = ""
.Subject = "Blah Blah"
.Item.Send
End With
End With
'select the original ActiveCell
rng.Select
End With
'Activate the sheet that was active before you run the macro
AWorksheet.Select
StopMacro:
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
'ActiveWorkbook.EnvelopeVisible = False
End Sub
I need to automate a report, this is automated almost completely, but in the end of the process it has a specific sheet to be send by email, and they ask for a special format.
I found code that will send the email, but it does not do the same thing that I do when using it manually, "send to mail recipient" button on the Excel ribbon.
This is the code that I am using:
Dim aSheet As Worksheet
Dim Sendrng As Range
Dim rng As Range
On Error GoTo StopMacro
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Sendrng = Worksheets("for_Mail_test").Range("A1:j42")
Set aSheet = ActiveSheet
With Sendrng
.Parent.Select
Set rng = ActiveCell
.Select
ActiveWorkbook.EnvelopeVisible = True
With .Parent.MailEnvelope
.Introduction = "This is test mail 2."
With .Item
.To = "ron#debruin.nl"
.CC = ""
.BCC = ""
.Subject = "My subject"
.Display
End With
End With
rng.Select
End With
aSheet.Select
StopMacro:
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
ActiveWorkbook.EnvelopeVisible = False
There are two problems:
1 it does not keep the same format that I am on the sheet
2 when I change the method from .send to .display it does not showup