What I am trying to do is basically you click a button, it brings up the Excel MailEnvelope to send an email, and you can then send it off to the relevant button email address'.
However one of the email addresses needs to be modifiable by the end user.
So I want a drop down where you select said email, and it then inputs that into the VBA code.
I know basically nothing about VBA and I could not find a way of doing this by searching around the web.
I figured I need some way of setting a variable to read a cell (the drop down cell), and then input that into the MailEnvelope Item.CC but I was struggling.
Any help would be appreciated.
This is what I have so far;
Sub Send_Range_Email()
' Select the range of cells on the active worksheet.
ActiveSheet.Range("B6:D302").Select
' Show the envelope on the ActiveWorkbook.
ActiveWorkbook.EnvelopeVisible = True
' Set the optional introduction field thats adds
' some header text to the email body. It also sets
' the To, CC and Subject lines.
With ActiveSheet.MailEnvelope
.Introduction = ""
.Item.To = "Email 0"
.Item.Subject = "Email Tracker Results"
.Item.CC = "Email 1" & text input here & "Email 2"
End With
End Sub
When using formulas, if you want to put a variable in there, just break it apart and add in the variable. As commented,
.Item.CC = "email 1" & "," & Range("A1").Value & ", " & "Email 2"
So to make super clear, say we want to add A1's value in this string: str = The man lives in STATE all the time by doing str = "The man lives in " & Range("A1").Value & " all the time"
Related
I have a workbook that houses several macros, one takes a message body that has been entered into a specific cell (this changes every quarter based on what the business wants) and uses it as the email body that is generated by the macro. The body of the email is pre-formatted when entered into this cell and retains that formatting when I use the regular body in VBA but loses its formatting when I change it to HTMLBody. They recently changed the email message to include hyperlinks. Unfortunately, if I try to make the hyperlinks look correct, it appears I need HTMLBody but then I lose the formatting of my message.
OriginalText = HF.Sheets("Email_Setups").Range("B9").Value
Link = "Open Requests"
CorrectedText = Replace(OriginalText, "Open Requests", Link)
'Create a new email item
Set objMail = objOutlook.CreateItem(olMailItem)
'From
'To
'objMail.To = Sheet1.Range("A" & lCounter).Value
'Cc
'objMail.CC = Sheet1.Range("B" & lCounter).Value
'Subject
objMail.Subject = HF.Sheets("Email_Setups").Range("A9").Value & " " & Tar_Num
'Email Body
objMail.HTMLBody = CorrectedText
With this using the HTMLBody, the formatting from the original cell text is lost. Is there a way to retain it without having to enter the whole message body in HTML code (since it changes every time and needs to be something the business can do without me coding it) and still get the desired result of the hyperlink? Thanks in advance!
So currently under specific conditions I have this InputBox poping up for the user to enter notes into and then that box fill those notes to a cell. What I would like to try to do is if notes are already in the cell that the InputBox is filling notes to, those notes will already appear in the input field so that the user can add to them. I have no idea if this can be done and could not find anything on google. If this cannot be done ill just use a user form insted.
The current code that brings up the input box is below, "notes" is the feild that the InputBox fills to:
If InStr(OPs, "Incomplete") > 0 Or InStr(OPs, "Miss") Then
notes.Interior.Color = RGB(255, 200, 0)
If notes = "" Then
Do While notes = ""
notes = notes & InputBox("You must imput notes for " & Desc & " !", "Notes")
Loop
End If
End If
Use the Default parameter of InputBox:
Something like:
notes = notes & InputBox("You must input notes for " & Desc & " !", "Notes", notes)
However, if you use this, you don't want to use the notes = notes & ... as the string field will quickly overfill.. so perhaps just:
notes = InputBox("You must input notes for " & Desc & " !", "Notes", notes)
I'm trying to send an email to recipients when a specific value of a cell is selected. If the value is 'new' then it gets sent to a predefined email. if the value is 'pending' or otherwise then it gets sent to a user entered email in another cell. I'm using this currently but i'm not sure how to modify it to look at the other cell.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myToAdd As String
If Target.Column = 6 Then
If Target.Value = "New " Then
myToAdd = "email#hotmail.com;"
ElseIf Target.Value = "Pending review" Then
myToAdd = 'I want this to look at another cell for the email
End If
With CreateObject("Outlook.Application").createitem(0) '0 will create a new email item
.To = myToAdd
.Subject = "A change request/project support has been request"
.Body = "Dear User," & vbNewLine & vbNewLine & "This is my email body "
.Display
End With
End If
You can use Cells([row],[col]) to reference other cells in the active worksheet.
On the other hand, if the worksheet is not active you can use Sheets([sheetname]).Cells([row],[col]).
#Cullen gave a good answer, also you can use:
SomeSheet.Range(<<cellreference)
e.g.
Sheets("CoolSheet").Range("A6")
I have an Excel spreadsheet of contacts. I want to set a drop-down list that sends an email to the specific person I choose and returns the contact info in the body of the email.
I don't know how to get the email to auto-populate and right now, the email that pops up has "true" in the body for the contact info rather than returning the text value in the cell.
Sub DropDown7_Change()
Dim answer As String
answer = MsgBox("Are you sure you want to assign this lead?", _
vbYesNo, "Send Email")
' Above code informs the user that an automated email will be sent
'Code uses the users answer to either carryout the generated email process or to not save the changes.
If answer = vbNo Then Cancel = True
If Cancel = True Then Exit Sub
If answer = vbYes Then
'Connects to outlook and retrieves information needed to create and send the email.
Set OutlookApp = CreateObject("Outlook.Application")
Set OlObjects = OutlookApp.GetNamespace("MAPI")
Set newmsg = OutlookApp.CreateItem(olMailItem)
'Contains the email address of the person receiving the email.
newmsg.Subject = "Lead Assigned to You" 'Sets the automated subject line to the email
newmsg.Body = "Hello," & vbNewLine & _
"You have been assigned a lead. Please follow up with the contact" & vbNewLine & _
ActiveCell.Offset(0, 3).Range("K5").Select
ActiveCell.Offset(0, 6).Range("K5").Select
ActiveCell.Offset(0, 7).Range("K5").Select
'Above code has the body of the automated email
newmsg.Display
End If
End Sub ' End of function
If you are trying to get the values that are Offset to Range("K5") , then you need to use the Offset with .Value , like this Range("K5").Offset(0, 3).Value , this will get the value 3 columns to the right of Cell "K5".
The code below, will add the values from 3 cells with Columns offset to cell "K5" to you email body:
Sub DropDown7_Change()
Dim answer As String
answer = MsgBox("Are you sure you want to assign this lead?", _
vbYesNo, "Send Email")
' Above code informs the user that an automated email will be sent
'Code uses the users answer to either carryout the generated email process or to not save the changes.
If answer = vbNo Then
Exit Sub
Else
If answer = vbYes Then
'Connects to outlook and retrieves information needed to create and send the email.
Set OutlookApp = CreateObject("Outlook.Application")
Set OlObjects = OutlookApp.GetNamespace("MAPI")
Set newmsg = OutlookApp.CreateItem(olMailItem)
'Contains the email address of the person receiving the email.
newmsg.Subject = "Lead Assigned to You" 'Sets the automated subject line to the email
newmsg.body = "Hello," & vbNewLine & _
"You have been assigned a lead. Please follow up with the contact" & vbNewLine & _
Range("K5").Offset(0, 3).Value & vbNewLine & _
Range("K5").Offset(0, 6).Value & vbNewLine & _
Range("K5").Offset(0, 7).Value & vbNewLine
'Above code has the body of the automated email
newmsg.Display
End If
End If
End Sub
I am selecting an email address from a combobox using a form in Excel and want to split the email address and display only first name from the selected email address. Please check the code as below:
Private Sub UserForm_Initialize()
Me.CboBugSts.List = Array("Open-Crtical", "Open-ShowStopper")
Me.CboCC.List = Array("Nandesh.kalyankar#wipro.com")
Me.CboToEmail.List = Array("firstName.LastName#Email.com")
Me.CboGrtngs.List = Array("Hi", "Dear")
TxtSubj.Value = "Bug notification logged by " & Environ("Username") & " - " & Now
End Sub
Private Sub TxtMsg_Change()
arTemp = Split(Me.CboToEmail.Text, "#")
Fname = arTemp(LBound(arTemp))
TxtMsg.Text = Me.CboGrtngs.Text & " " & Fname
End Sub
Here Me.CboToEmail.text is a value that user selects from a combobox text, and then I split the email address and concatenate it with another combobox text Me.CboGrtngs.text, but it shows the whole email, not just the first name from email.
Not sure (not a vba programmer), but since CboToEmail is defined as a list or array (with one item):
Me.CboToEmail.List = Array("firstName.LastName#Email.com")
Don't you need to extract the first element here, instead of calling .Text on the whole list?
arTemp = Split(Me.CboToEmail.Text, "#")
If not, what's the value of Me.CboToEmail.Text?