Automate Email Message Text Dependent on Text Box value - excel

I am trying to send an email through VBA that is populated with information from a userform, I can do this quite easily with the code that I have below,
The only problem I am having is some of the information is only required when it is a certain value in the form, for example, the colour will be standard as "Black" unless specified otherwise.
If the colour is set to "Black" in my userform I do not want to include it on the email, I only want it to show if they want another colour such as "White"
Is there an if statement that skips the whole line and continues the email if the text box is "Black" and will only include it on the email if it something other than black?
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = csemail_txt.Value
.CC = ""
.BCC = ""
.Subject = "Quote Reference: " & Quotenum_txt.Value
.Body = "Hello" & csname_txt.Value & vbNewLine & _
"Thank you for your enquiry, please find your quote below: " & vbNewLine & vbNewLine & _
"Product Code: " & ProductCode_Combo.Value & vbNewLine & _
"Dimensions: " & dimension_txt.Value & vbNewLine
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
minor correction ;-)

Option Explicit
Sub OutStr()
ColorStr = "white"
BodyStr = "Hello " & csname_txt.Value "!" & vbNewLine
If ColorStr = "black" Then
'We don't talk about the color if the color is black ;-)
'see https://www.google.com/search?q=henry+ford+color+black
'BodyStr = "The color will be black as you know!"
Else
BodyStr = BodyStr & "The color will be " & ColorStr & " as requested!" & vbNewLine
End If
BodyStr = BodyStr & "Your Board will have " & NrOfWheels & " wheels as requested!" & vbNewLine
if feature1 then BodyStr = BodyStr & "Let's talk about feature 1!" & vbNewLine
if feature2 then BodyStr = BodyStr & "Let's talk about feature 2!" & vbNewLine
if feature3 then BodyStr = BodyStr & "Let's talk about feature 3!" & vbNewLine
MsgBox BodyStr
End Sub

Related

Generating HTML Mail for Outlook with scaled background image via Excel VBA

I am trying to generate several Mails with a text body and a scaled background image. Everything works out, the only problem is, that the background image appears many times instead of one time with the correct scale.
Setting a background image using HTML in an outlook email using Excel VBA
I've tried this approach, but the
MyHTML = "<body background=""cid:Pic1.jpg""; center top no-repeat;>"
code does not work for me.
Sub FORMATIERUNG_TESTEN()
Dim objOLOutlook As Object
Dim objOLMail As Object
Dim lngMailNr As Long
Dim lngZaehler As Long
Dim strAttachmentPfad1 As String
Dim a As String
Dim image As String
Dim strbody As String
Dim MyHTML As String
Dim MyText As String
On Error GoTo ErrorHandler
Set objOLOutlook = CreateObject("Outlook.Application")
lngMailNr = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
strAttachmentPfad1 = "C:\Users\User\Downloads\Pic1.jpg"
'running through every not empty line of column a to get the email adresses
For lngZaehler = 2 To lngMailNr
If Cells(lngZaehler, 1) <> "" Then
Set objOLMail = objOLOutlook.CreateItem(olMailItem)
On Error Resume Next
With objOLMail
.To = Cells(lngZaehler, 1)
.CC = Cells(lngZaehler, 2)
.BCC = ""
.Sensitivity = 0
.Importance = 0
.Subject = "Test"
'creating the text body of the mail
strbody = "<font size=""2,9"" face=""Source Sans Pro"" color=""#2F5496"">" & _
"TEXT TEXT TEXT TEXT TEXT" & "</font>"
'here is the problem: I get the background image in the mail, but the "no repeat" command does not work
MyHTML = "<body background=""cid:Pic1.jpg""; center top no-repeat;>"
.HTMLBody = MyHTML & "<br>" & "<br>" & "<br>" & strbody & "<br>" & "<br>" & "<br>" & "<br>" & "<img src = 'Signature.jpg' >"
.Display
'.Send
.Attachments.Add strAttachmentPfad1
End With
Set objOLMail = Nothing
End If
Next lngZaehler
Set objOLOutlook = Nothing
Exit Sub
ErrorHandler:
MsgBox Err.Number & " " & Err.Description & " " & Err.Source, _
vbInformation, "Error"
End Sub

Adding attachment to email

I want to create a button that saves a worksheet in pdf format, Attaches to a new mail and send it.
i can create the pdf, save the pdf on my desktop, create and e-mail. but the PDF is never attached. Where am I going wrong?
Dim pdfName As String
pdfName = PONumberLabel.Caption ' add PO number on label to a variable
' create pdf and save to desktop
ChDir "C:\Users\roanderson\Desktop" ' selects directory to save
Sheet4.ExportAsFixedFormat _
Type:=xlTypePDF, _
OpenAfterPublish:=True, _
Quality:=xlQualityStandard, _
Filename:="C:\Users\roanderson\Desktop\" & pdfName ' directory put t
' sending email for approval
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
On Error Resume Next
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xMailBody = "Please approve PO Number" & " " & PONumber & vbNewLine & vbNewLine & _
"Cost Centre:" & " " & costcentre & vbNewLine & _
"Description:" & " " & description & vbNewLine & _
"Currency:" & " " & POCurrency & vbNewLine & _
" Total:" & " " & total
On Error Resume Next
With xOutMail
.To = "Ross.anderson#work.com"
.CC = ""
.BCC = ""
.Subject = "PO Number " & PONumber & " " & "Approval"
.Body = xMailBody
.Display 'or use .Send
.Attachments.Add "C:\Users\roanderson\Desktop\" & pdfName
.VotingOptions = "Accept;Reject"
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
Everything works except attaching pdf to email.
Change the Statement
.Attachments.Add "C:\Users\roanderson\Desktop\" & pdfName
To
.Attachments.Add "C:\Users\roanderson\Desktop\" & pdfName & ".pdf"
As it seems like pdfname does not have the full name with Extension.

How can I call a cell value within a string?

I'm trying to set up a macro to send an email, updating people on a due date timeline. This email should pull dates from specific cells and place them into the message body. So far I'm able to set up the email to read the way I want, but am having trouble calling the dates.
I'm super new at vba and this may not be a function that's possible, but any workarounds would be appreciated! Thanks!
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Dim rev_1 As Date, rev_2 As Date, final_due As Date
rev_1 = Range(I2)
rev_2 = Range(K2)
final_due = Range(M2)
strbody = "XXXXXXX" & vbNewLine & vbNewLine & _
"This is an update on your project dates:" & vbNewLine & vbNewLine & _
"Review 1 Suspense: CALL CELL I2 HERE" & vbNewLine & _
"Review 2 Suspense: CALL CELL K2 HERE" & vbNewLine & _
"Final Due Date: CALL CELL M2 HERE" & vbNewLine & vbNewLine & _
"Acoording to this timeline, you are AHEAD/ON TIME/BEHIND." & vbNewLine & vbNewLine & _
"If you have any questions, please contact XXXX." & vbNewLine & vbNewLine & _
On Error Resume Next
With OutMail
.To = ""
.CC = ""
.BCC = ""
.Subject = "Upcoming Project - Timeline Update"
.Body = strbody
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
hello you could do like this
"Review 1 Suspense: " & range("I2") & vbNewLine & _

add text formatting to specific texts in excel generated email

I'm using excel vba to set certain texts in the email body bold
here is my code
xFirstName = Sheets("Sheet1").Range("C2")
xMembershipDate = Sheets("Sheet1").Range("G2")
On Error Resume Next
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xMailBody = "Hello " & xFirstName & vbNewLine & _
"Your membership has been verified last " & xMembershipDate & vbNewLine & _
"" & vbNewLine & _
"Thank you."
I'd like to set xFirstName and xMembershipDate to appear as bold in the email body. Any help is greatly appreciated. Thank you.
You'd have to use HTML tags.
xFirstName = Sheets("Sheet1").Range("C2")
xMembershipDate = Sheets("Sheet1").Range("G2")
On Error Resume Next
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
With xOutMail
.HTMLBody = "<p>Hello <b>" & xFirstName & "</b></p>" & _
"<p>Your membership has been verified last <b>" & xMembershipDate & "</b></p>" & _
"" & vbNewLine & _
"<p>Thank you.</p>"
End With

Send Multiple Emails Via VBA

I wonder whether someone could help me please.
I'm trying to write a script which send multiple emails to addressees on a spreadsheet, with various other pieces of information.
I've started to use a solution from Ron de Bruin (below).
Sub Email()
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Dim Src As Worksheet
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
Set Src = ThisWorkbook.Sheets("List")
On Error GoTo cleanup
Src.Select
For Each cell In Columns("C").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value Like "?*#?*.?*" Then
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = cell.Value
.Subject = "Splunk Access"
.Body = "Hi " & Cells(cell.Row, "A").Value _
& vbNewLine & vbNewLine & _
"I have created an account: Production." & _
vbNewLine & vbNewLine & _
"Your username and password for this environment is:" & _
vbNewLine & vbNewLine & _
"Username: " & Cells(cell.Row, "B").Value & _
vbNewLine & _
"Password: " & Cells(cell.Row, "E").Value & _
vbNewLine & vbNewLine & _
"Please log in at your earliest convenience and change your password to a more secure one. " & _
vbNewLine & vbNewLine & _
"You can do this by clicking on your name on the top menu and select ‘Edit Account’." & _
vbNewLine & vbNewLine & _
"You can use this link to get to the log in page for this environment: " & _
vbNewLine & vbNewLine & _
"PROD: right/en-US/account/logout " & _
vbNewLine & vbNewLine & _
"Many thanks and kind regards"
.send
End With
On Error GoTo 0
Set OutMail = Nothing
End If
Next cell
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub
This script works, but I then receive the 'Outlook' security ,message, which with over 100 recipients, isn't practical to keep pressing "Ok" to send the email.
So following more research I changed:
.send
to
.Display
Application.Wait (Now + TimeValue("0:00:01"))
Application.SendKeys "%"
But the problem I have is that the email is created, but isn't sent. Again not practical to keep pressing "Send" for over 100 users.
I then tried a CDO solution, but I ran into problems with the SMTP address because I'm using my works Microsoft Exchange which I'm not an administrator for, and so don't have any of the SMTP details.
I just wondered whether someone may be able to look a this please, and offer some guidance on how I can create the macro to run automatically.
Many thanks and kind regards
Chris
All,
I managed to get this working with the following:
Sub Email()
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Dim Src As Worksheet
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
Set Src = ThisWorkbook.Sheets("List")
On Error GoTo cleanup
Src.Select
For Each cell In Columns("C").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value Like "?*#?*.?*" Then
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = cell.Value
.Subject = "Access"
.Body = "Hi " & Cells(cell.Row, "A").Value _
& vbNewLine & vbNewLine & _
"I have created an account for you" & _
vbNewLine & vbNewLine & _
"Your username and password for this environment is:" & _
vbNewLine & vbNewLine & _
"Username: " & Cells(cell.Row, "B").Value & _
vbNewLine & _
"Password: " & Cells(cell.Row, "E").Value & _
vbNewLine & vbNewLine & _
"Please log in at your earliest convenience and change your password to a more secure one. " & _
vbNewLine & vbNewLine & _
"You can do this by clicking on your name on the top menu and select 'Edit Account'." & _
vbNewLine & vbNewLine & _
"You can use this link to get to the log in page for this environment: " & _
vbNewLine & vbNewLine & _
"PROD: https://right/en-US/account/logout " & _
vbNewLine & vbNewLine & _
"Many thanks and kind regards"
' .send
.Display
Application.Wait (Now + TimeValue("0:00:02"))
Application.SendKeys "%s"
Application.SendKeys "+o"
End With
On Error GoTo 0
Set OutMail = Nothing
End If
Next cell
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub
I found through further testing, that a automatic pop up appeared when the 'Send' button was clicked by this command Application.SendKeys "%s", so I added Application.SendKeys "+o2, to automatically click "OK".
Kind regards
Chris
try
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
this is of course using .Send
make sure to turn them back on at end of sub

Resources