When my email generates it doesn't have the line breaks despite using the "& vbCrLf &" code. I've tried using the <br> and <p> but I get compile errors every time.
Can someone please take a look at my code and help a brother out?
Dim strbody As String
Set xOutlookObj = CreateObject("Outlook.Application")
Set xEmailObj = xOutlookObj.CreateItem(0)
With xEmailObj
.Display
.to = xMemberName5
.CC = ""
.Subject = "Annual Review " + xMemberName + " " + "Cust " + xMemberName3
strbody = "<p style='font-family:calibri;font-size:11pt;color:rgb(31,78,121)'>" + xMemberName7 + "," _
& vbCrLf & vbCrLf & "Our records indicate that " + xMemberName + " is due for an annual pricing review. We are seeking an overall impact of " + xMemberName6 + "% increase to the rates. Updated Tariff page is attached." _
& vbCrLf & "If there are any pricing issues which need to be addressed, please get back to me no later than " & CDate(Date + 7) & "." _
& vbCrLf & vbCrLf & "Otherwise, the attached new pricing will be effective " + xMemberName4 + ". I encourage you to visit with your customer and deliver the new pricing ASAP." & .HTMLBody & "</body>"
.HTMLBody = strbody
Just include the <br>directly in your string as you did with <p> already
strbody = "<p style='font-family:calibri;font-size:11pt;color:rgb(31,78,121)'>" & xMemberName7 & "," _
& "<br><br>Our records indicate that " & xMemberName & " is due for an annual pricing review. We are seeking an overall impact of " & xMemberName6 & "% increase to the rates. Updated Tariff page is attached." _
& "<br>If there are any pricing issues which need to be addressed, please get back to me no later than " & CDate(Date + 7) & "." _
& "<br><br>Otherwise, the attached new pricing will be effective " & xMemberName4 & ". I encourage you to visit with your customer and deliver the new pricing ASAP." & .HTMLBody & "</body>"
And I recommend to use & instead of + to concatenate your variables with strings.
Related
I am taking Applied/Advanced Financial Analysis class and our assignment this week is to display an amortization schedule in a messagebox using 3 (or 4) input boxes.
I reached out to my professor, but I am not knowledgable enough to follow the directions.
I can get all four input boxes to show up and the messagebox as well, but it will not display the loop. I am trying to store the calculation for the loop in a variable, but I have no clue how to do that.
Sub PaymentScheduleCalculator()
Dim PV As Single '10000
Dim years As Single '2
Dim frequency As Double '12
Dim rate As Variant '4% APR
Dim Ppmt As Double
Dim Ipmt As Double
Dim Pmt As Single 'for pmt after each year
Dim i As Integer 'designation for loop
Dim Temp As Integer
Dim TempVars!
For i = 1 To n * frequency
Pmt = PV * rate / frequency
TempVars! = Temp & vbNewLine & i & _
vbTab & FormatCurrency(PV, 2) & _
vbTab & FormatCurrency(Pmt, 2) & _
vbTab & FormatCurrency(Ipmt, 2) & _
vbTab & FormatCurrency(-Ipmt, 2)
PV = PV - Pmt + Ipmt
Next i
PV = InputBox("How much money do you want to borrow?", "Payment Calculator", 10000)
years = InputBox("If you borrow " & FormatCurrency(PV) & " - how many years do want to borrow the money for?", "Payment Calculator", 2)
rate = InputBox("If you borrow " & FormatCurrency(PV) & " for " & years & " years, " & "what interest rate are you paying?", "Payment Calculator", 0.04)
If Right(rate, 1) = "%" Then
rate = Val(Left(rate, Len(rate) - 1) / 100)
Else
rate = rate
End If
frequency = InputBox("If you borrow " & FormatCurrency(PV) & " at " & FormatPercent(rate) & "," & " for " & years & " years, " & _
"how many payment intervals are there per year?", "Payment Calculator", 12)
'runs fine until here but does not display the loop
MsgBox "Loan Amount " & FormatCurrency(PV) & _
vbNewLine & "Number of Payments " & years * frequency & _
vbNewLine & "Interest Rate " & FormatPercent(rate) & _
vbNewLine & _
vbNewLine & "PMT # " & vbTab & "Balance " & vbTab & "Payment " & vbTab &
"Interest " & vbTab & "Capital " & _
vbNewLine & RepeatCalc, , "Payment Calculator"
End Sub
There are several things wrong with your code...as others have pointed out as well.
First of, you want to run a For...next loop by using
For i = 1 To n * frequency
'some code
next
The loop is initiated before those variables 'n' & 'frequency' have been assigned a value. Although your intention was to set 'frequency' by means of an Input box, 'n' is not set anywhere and will hence always be 0. The result is therefore in essence that the loop reads:
For i = 1 To 0 * 12 'which results in the loop not running at all
'some code
next
So, properly set 'n' to whatever it needs to be or provide an input dialog for it.
Moreover, the actual intention of this loop is to extend the displayed text in the message box.
If you want to do that, you will need to start building the string first.
According to your code your text should begin with:
"Loan Amount " & FormatCurrency(PV) & _
vbNewLine & "Number of Payments " & years * frequency & _
vbNewLine & "Interest Rate " & FormatPercent(rate) & _
vbNewLine & _
vbNewLine & "PMT # " & vbTab & "Balance " & vbTab & "Payment " & vbTab & "Interest " & vbTab & "Capital "
So if you assign that text to a variable you can then extend the text using the loop, like so:
Dim msgText As String
msgText = "Loan Amount " & FormatCurrency(PV) & _
vbNewLine & "Number of Payments " & years * frequency & _
vbNewLine & "Interest Rate " & FormatPercent(rate) & _
vbNewLine & _
vbNewLine & "PMT # " & vbTab & "Balance " & vbTab & "Payment " & vbTab & "Interest " & vbTab & "Capital "
'now the loop extends 'msgText'
For i = 1 To n * frequency
Pmt = PV * rate / frequency
msgText = msgText & vbNewLine & Str(i) & _
vbTab & FormatCurrency(PV, 2) & _
vbTab & FormatCurrency(Pmt, 2) & _
vbTab & FormatCurrency(Ipmt, 2) & _
vbTab & FormatCurrency(-Ipmt, 2)
PV = PV - Pmt + Ipmt
Next i
msgText = msgText & vbNewLine & RepeatCalc
Once that is completed, the whole string is contained in variable msgText
You can then initiate the MsgBox with that text.
MsgBox msgText, vbOKOnly, "Payment Calculator"
One more thing, you have an InputBox for rate and you seem to try and remove the '%' sign at the end of the string in case somebody puts in a percentage with a sign. (good thinking in itself..:-))
Although this works, what it ends up with is a String type variable and, as rate is declared as Variant it has no issues with that...
But it will screw up your calculation in your For..next loop, as Pmt = PV * rate / frequency can only work using values (i.e. Double\Integer etc.)
What you should do is declare rate as a Double and ensure that the result of your '%' removal will be converted to a Double value by means of -for example- using CDbl(Val(Left(rate, Len(rate) - 1) / 100))
So, your code, with some amendments should look more or less like the below. Mind you, you may have to ensure that all variables (including 'n') are properly set to ensure the calculation is correct. As, for example, Ipmt is not calculated anywhere...
Altered code:
Sub PaymentScheduleCalculator()
Dim PV As Single '10000
Dim years As Single '2
Dim frequency As Double '12
Dim rate As Double '4% APR
Dim strRate As String
Dim Ppmt As Double
Dim Ipmt As Double
Dim Pmt As Single 'for pmt after each year
Dim i As Integer 'designation for loop
Dim Temp As Integer
n = 1 'has to be properly set to relevant value?
PV = InputBox("How much money do you want to borrow?", "Payment Calculator", 10000)
years = InputBox("If you borrow " & FormatCurrency(PV) & " - how many years do want to borrow the money for?", "Payment Calculator", 2)
strRate = InputBox("If you borrow " & FormatCurrency(PV) & " for " & years & " years, " & "what interest rate are you paying?", "Payment Calculator", 0.04)
If Right(strRate, 1) = "%" Then
rate = CDbl(Val(Left(strRate, Len(strRate) - 1) / 100))
Else
rate = CDbl(strRate)
End If
frequency = InputBox("If you borrow " & FormatCurrency(PV) & " at " & FormatPercent(rate) & "," & " for " & years & " years, " & _
"how many payment intervals are there per year?", "Payment Calculator", 12)
Dim msgText As String
msgText = "Loan Amount " & FormatCurrency(PV) & _
vbNewLine & "Number of Payments " & years * frequency & _
vbNewLine & "Interest Rate " & FormatPercent(rate) & _
vbNewLine & _
vbNewLine & "PMT # " & vbTab & "Balance " & vbTab & "Payment " & vbTab & "Interest " & vbTab & "Capital "
'now extends the 'msgText' variable
For i = 1 To n * frequency
Pmt = PV * rate / frequency
msgText = msgText & vbNewLine & Str(i) & _
vbTab & FormatCurrency(PV, 2) & _
vbTab & FormatCurrency(Pmt, 2) & _
vbTab & FormatCurrency(Ipmt, 2) & _
vbTab & FormatCurrency(-Ipmt, 2)
PV = PV - Pmt + Ipmt
Next i
msgText = msgText & vbNewLine & RepeatCalc
MsgBox msgText, vbOKOnly, "Payment Calculator"
End Sub
CurrentRegion is extracted but not shown in email body
Sub Draft()
Dim myDataRng As Range
Set myDataRng = Range("c2:c2")
Dim data As String
data = Range("B11").CurrentRegion.Select
For Each Cell In myDataRng
If Cell.Value > 0 Then
Dim objOutlook As Object
Set objOutlook = CreateObject("outlook.application")
Dim objEmail As Object
Set objEmail = objOutlook.CreateItem(olMailItem)
Range("K2").Select
With objEmail
.SentOnBehalfOfName = "accounting#test.co.uk"
.to = ActiveCell.Offset(0, 1).Value
.Subject = ActiveCell.Offset(7, 0).Value
.htmlbody = "Supplier Code " & " " & Cell.Offset(0, 0).Value & "<br>" & "Supplier Name: " & " " & Cell.Offset(1, 0).Value & "<br>" & "Currency " & " " & Cell.Offset(2, 0).Value & "<br>" & "<br>" & "Dear Supplier," & "<br>" & "<br>" & "A payment has been issued to you, as detailed below. " & "<br>" & data & "<br>" & "<br>" & "Kind Regards,<br>Johnny Grif <br>Accounts Assistant/Accounts Department" & "<br>" & "T:+44(0)1234 567 890" & "<br>" & "E:accounting#test.co.uk"
.Save
End With
Set objOutlook = Nothing
End If
Next Cell
Set myDataRng = Nothing
Set objEmail = Nothing: Set objOutlook = Nothing
MsgBox "Please check pyament advice in your draft folder!"
End Sub
The final outcome is like this.
Dear Supplier,
A payment has been issued to you, as detailed below.
True
Kind Regards,
Johnny Grif
Accounts Assistant/Accounts Department
T:+44(0)1234 567 890
E:accounting#test.co.uk
Below is the output:
The following code is the cause of the problem:
.htmlbody = "Supplier Code " & " " & Cell.Offset(0, 0).Value & "<br>" & "Supplier Name: " & " " & Cell.Offset(1, 0).Value & "<br>" & "Currency " & " " & Cell.Offset(2, 0).Value & "<br>" & "<br>" & "Dear Supplier," & "<br>" & "<br>" &
"A payment has been issued to you, as detailed below. " & "<br>" & data & "<br>" & "<br>" & "Kind Regards,<br>Johnny Grif <br>Accounts Assistant/Accounts Department" & "<br>" & "T:+44(0)1234 567 890" & "<br>" & "E:accounting#test.co.uk"
First of all, the message body markup should be represented by a well formed HTML document/string.
Second, if you need to break the line in the code you need to use the underscore _ character in the code to get the lines assembled correctly.
Third, I'd suggest breaking the code and trying to assemble the final from multiple pieces, so you could find out why the code doesn't work correctly.
I am trying to change the text so certain values from cells are either bold, underlined, red, or otherwise stand out from the surrounding text in the body of the email.
How can I do that?
For i = 10 To 18
If Not Cells(i, "A").Text = vbNullString Then
'Add to growing string
concatString = concatString + Cells(i, "A").Text & ", " & _
Cells(i, "B").Text & vbCr
concatString = concatString + "Assignment/Zone: " & _
Cells(i, "C").Text & vbNewLine & vbCr
End If
Next i
myMail.Body = Range("B2") & " Shift" & " - " & Format(Date, "mmmm dd, yyyy") _
& vbNewLine & vbNewLine & "Sergeant: " & Range("A6") & ", " & Range("B6") & _
vbNewLine & " Status: " & Range("C6") _
& vbNewLine & vbNewLine & "Corporal: " & Range("A8") & ", " & Range("B8") & _
vbNewLine & " Status: " & Range("C8") _
& vbNewLine & vbNewLine & "Assigned Deputies" & vbNewLine & vbNewLine & concatString
You need to look into using HTML-formatted content to apply the colors etc you want:
Dim oApp As Object, oMail As Object
Set oApp = CreateObject("outlook.application")
Set oMail = oApp.createitem(0)
oMail.Display
oMail.htmlBody = "<h1>This is a heading</h1>" & _
"<p style='color:#F00'>Some red text</p>" & _
"<p><u>Underlined</u></p>" & _
"<p><b>Bold</b></p>" & _
"<p><i>Italic</i></p>"
I needed to use <br> to put the resultant answer in the email body. <p> creates a new PARAGRAPH, while <br> just puts it on the next line.
& "<br><b><u>Status:</u></b>"
gives:
& "Status:" &
Instead of:
& "<p><b><u>Status:</u></b>"
Which gives:
& "Status:"
Thank you for your help!
I am trying to add in an if statement half way through a HTML body of text I am using in VBA to send an e-mail.
I need to work out how to get the code to add extra hyperlinks if a cell in on one of the tabs has a value, there could be up to five that may need to be added.
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.Display
.To = Accounts
.Subject = "Consolidated Account Statement " & myPolicynumber & " - " & mypolicyname
If myhyperlink2 = "" Then
.HTMLBody = "<HTML><BODY>" & "<FONT-size=""11.0pt"">" & "Hi Accounts" & "<br><br>" & _
" The Account Statement" & _
" for " & myPolicynumber & " (" & mypolicyname & ") is ready to be created. " & "<br><br>" & "<br><br>" & _
" The following Medical Extra Premium also need booking " & "<br><br>"
If Worksheets("FOR PA").Cells(13, 39).Value = "Medicals" Then
" Medical Extra Premium 1" & "<br><br>" & _
" Medical Extra Premium 2" & "<br><br>" & _
" Medical Extra Premium 3" & "<br><br>" & _
" Medical Extra Premium 4" & "<br><br>" & _
" Medical Extra Premium 5" & "<br><br>" & _
" Kind Regards," &.HTMLBody
.Send
Else
.HTMLBody = "<HTML><BODY>" & "<FONT-size=""11.0pt"">" & "Hi Accounts" & "<br><br>" & _
" The Initial" & " and the " & "Final & are ready to be put on the Account Statement " & _
" for " & myPolicynumber & " (" & mypolicyname & ") is ready to be created. " & "<br><br>" & _
" Kind Regards," & .HTMLBody
.Send
End If
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
You're doing the right thing, but you're missing a .HTMLBody = before one of the options:
If Worksheets("FOR PA").Cells(13, 39).Value = "Medicals" Then
.HTMLBody = " Medical Extra Premium 1" & "<br><br>" & _
" Medical Extra Premium 2" & "<br><br>" & _
I have an Excel macro which creates an Outlook appointment. I can make everything work except I need to set the Time Zone as "Eastern". Some of my co-workers live in other time zones and I want to make sure the appointment is set at the correct time for them. Here is the code I currently have. How do I set the time zone to Eastern (US & Canada)?
Set objOL = CreateObject("Outlook.Application")
Set objItem = objOL.CreateItem(1)
With objItem
.StartTimeZone = "Eastern"
.Start = Range("B4").Text & " " & Range("C4").Text
.End = Range("B4").Text & " " & Range("D4").Text
.Body = "Centra Link: " & Range("K4") & vbCrLf & vbCrLf & " Phone: " & Range("I4") & vbCrLf & vbCrLf & "Lead facilitator: " & Range("E4") & vbCrLf & "Co-facilitator: " & Range("F4") & vbCrLf & vbCrLf & Range("MISC_HEADER") & ": " & Range("H4")
.Location = Range("I4") & ", Leader Code: " & Range("J4")
.alldayevent = False
.Subject = Range("A4")
.ReminderMinutesBeforeStart = 30
.ReminderSet = True
.Save
End With
Set objItem = Nothing
Set objOL = Nothing
MsgBox "An appointment has been created for " & Range("A4") & " on " & Range("B4"), vbOKOnly, "Calendar Appointment"
It is not that easy, as to say
.StartTimeTone = "Eastern"
look here: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook._appointmentitem.starttimezone.aspx
you see, you would have to create a TimeZone object, like here
How to Modify Properties (Time Zone) of Recurring Appointments in Outlook 2010 VBA
look at the answear of siddharth rout.