JuicyMusic
Board Regular
- Joined
- Jun 13, 2020
- Messages
- 210
- Office Version
- 365
- Platform
- Windows
Hello everyone, I have a code that generates emails based on email addresses of recipients in Col A - and CC: in Col A - and BCC: in Col C.
This code uses two text boxes: 1) Text box for the email header........ 2) Text box for the email body. This code works great - but I need to adjust it to work on a different spreadsheet.
HERE IS WHAT I NEED
I need to see how to adjust this code so that whatever the range of cells that the user will be selecting - on any selected sheet - will be pasted into the body of the generated email.
The range will always vary - from 1 to 15 columns - from 2 to 50 rows. This may not always be the case and this is why I'm asking for "selected range" - with all formatting to come across as well.
No text boxes for this one.
I hope the code below is helpful to you. I can't use XL2BB here at work - so I'll insert a snippet of an example range. I don't think you need that but I'll add anyways. I selected a range on the snippet so you can see columns and rows will vary. Thank you so much!.....I am "CG" in the code below.
This code uses two text boxes: 1) Text box for the email header........ 2) Text box for the email body. This code works great - but I need to adjust it to work on a different spreadsheet.
HERE IS WHAT I NEED
I need to see how to adjust this code so that whatever the range of cells that the user will be selecting - on any selected sheet - will be pasted into the body of the generated email.
The range will always vary - from 1 to 15 columns - from 2 to 50 rows. This may not always be the case and this is why I'm asking for "selected range" - with all formatting to come across as well.
No text boxes for this one.
I hope the code below is helpful to you. I can't use XL2BB here at work - so I'll insert a snippet of an example range. I don't think you need that but I'll add anyways. I selected a range on the snippet so you can see columns and rows will vary. Thank you so much!.....I am "CG" in the code below.
VBA Code:
Option Explicit
Sub EMAIL_Send_to_DistributionList()
Dim OutlookApp As Object
Dim MItem As Object
Dim cell As Range
Dim email_ As String
Dim cc_ As String, bcc_ As String
'CG deactivated the 3rd and 4th "As String" of the line of code above. Not needed because 2 TextBoxes were used instead. See last section of this code.
'subject_ As String, body_ As String
'CG added this section of code to create a error message box
If WorksheetFunction.CountA(Range("A2:A350")) = 0 Then
MsgBox "Please enter a minimum of one email address in Column A.", vbCritical, "Missing Email Address"
Exit Sub
End If
'CG added this section of code to create a Yes/No Message Box
Dim answer As Variant
answer = MsgBox("Are you ready to generate your email?", vbYesNo + vbQuestion, "KGC_Email Distribution List")
If answer = vbNo Then
Exit Sub
End If
'Create Outlook object
Set OutlookApp = CreateObject("Outlook.Application")
'Loop through the rows
For Each cell In Range("A2:A350").Cells.SpecialCells(xlCellTypeConstants)
email_ = cell.Value
cc_ = cell.Offset(0, 1).Value
bcc_ = cell.Offset(0, 2)
'CG deactivated the next two lines of code and used TexBoxes instead
' subject_ = cell.Offset(0, 3)
' body_ = cell.Offset(0, 4)
'Create Mail Item and send it
Set MItem = OutlookApp.CreateItem(0)
With MItem
.To = email_
.CC = cc_
.BCC = bcc_
'CG changed the following line of code to recognize the 2nd TextBox
.Subject = ActiveSheet.TextBoxes(2).Text
'CG changed the following line of code to recognize the 1st TextBox
.Body = ActiveSheet.TextBoxes(1).Text
'CG changed the following line of code from .Send to .Display temporarily
.Display
End With
Next
End Sub