How to VBA things for weekly email

cxm687

New Member
Joined
Nov 9, 2023
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Hi all

Learning VBA slowly on my own and I have managed to write a script for me to send multiple emails weekly, however there are 2 things I would like to do….

when referencing a cell in the email body which is a negative value, I would like that text to be red, and a positive value the text to be green.

attach my signature from Outlook to the bottom of each email. Any help with this would be fab!

Current script

Dim Edress As String, subj As String
Dim OutlookOBJ As Object, mItem As Object

Set OutlookOBJ = CreateObject(“Outlook.Application”)
Set mItem = OutlookOBJ.CreateItem(olMailItem)

With mItem

.To
.Cc
.subject
.attachment.add
.Body “text writing here” & sheet(“one”).Range(“A15”).Value & “more text here”
.Send
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
First build an HTML string to format your value...

VBA Code:
'build html string to format value
Dim txt As String
With Sheets("one").Range("A15")
    If .Value > 0 Then
        txt = "<span style='color: #00B050'>" & .Text & "</span>" 'green
    ElseIf .Value < 0 Then
        txt = "<span style='color: #FF0000'>" & .Text & "</span>" 'red
    Else
        txt = .Text
    End If
End With

Then use .HTMLBody instead of .Body...

VBA Code:
.HTMLBody = "<p>text writing here " & txt & " more text here.</p>"

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,223,878
Messages
6,175,141
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top