Controlling fonts in an email message

  • Thread starter Thread starter OssieMac
  • Start date Start date
O

OssieMac

Is there any way of controlling the fonts (like making part of a message
bold) in the body of an email message.

Currently the body of the message is concatenated from fields in a table and
calculated values in the code.
 
You can use CSS in the HTMLBody as pointed out by Daniel.

It works in Outlook, I've done it.
 
Thank you Alex and Daniel for your help. Also Chris for your suggestion but
unfortunately I have no idea what you are referring to. Alex and Daniel
pointed me in the right direction to get me started. However, I still needed
quite a lot of research to complete the task. For anyone else looking at
this, the following 2 sites probably had the most helpful info.

http://msdn.microsoft.com/en-us/library/aa171418(office.11).aspx

http://www.w3schools.com/html/html_formatting.asp

Also the following example that I have now developed might be helpful to
anyone else attempting to control the fonts in an email message.

The method I have used to either GetObject if Outlook is open or
CreateObject if Outlook is not open is to alleviate a problem. Documentation
says that CreateObject will not open multiple instances of Outlook. However,
if an email is still in the process of being sent when another CreateObject
is processed, then an additional Outlook process is created and the first one
does not get closed. If this occurs repeatedly, eventually everything
crashes. My workaround has been to only use CreateObject if not open then
display the Inbox to make it visible and then NOT close Outlook. When using
this method, setting the variables to nothing when finished with them does
not close outlook. (The user can close Outlook later and if they try to close
with unsent messages then they get an Alert.)

Also note the use of <br> in HTML code. It is a line feed. To get the VBA
line feeds (vbCrLf) recognised in HTML, the <pre> code needs to be used.
However, this kills wrap text so use the <br> code instead and wrap text
works fine.

To use the early binding method, in the VBA editor select Tools->References
and scroll down to Microsoft Outlook nn.0 Object Library where nn.0 is the
version number. Check the box. (Ensure you check the box and not just select
the row.)

Only tested in Office 2002 and 2003.

As always, I am interested in comments/suggestions to make it better.

Sub SendOutLookEmail_HTML()
'Uses HTML to format text in the body of a message.
'This code either uses existing instance of Outlook
'or if not already open, opens Outlook and keeps it open.
'Sends a message or Displays the message for editing
'depending on the state of bolDisplaySend.

'Following 3 lines for Early binding.
'Dimension as objects for late binding.
Dim objOutlook As Outlook.Application
Dim objNameSpace As Outlook.NameSpace
Dim objMail As Outlook.MailItem

'Dim objOutlook As Object 'For late binding
'Dim objNameSpace As Object 'For late binding
'Dim objMail As Object 'For late binding

'Following 3 lines only used if late binding used
'Const olfolderinbox = 6
'Const olMailItem = 0
'Const olFormatHTML = 2

Dim lngErrNumber As Long
Dim bolDisplaySend As Boolean

Dim strTo As String
Dim strSubject As String
Dim strCC As String
Dim strBCC As String
Dim strBody1 As String
Dim strBody2 As String
Dim strBody3 As String
Dim strBody4 As String
Dim strBody5 As String
Dim strBody6 As String

bolDisplaySend = True 'Set to False to send without editing.

strTo = "(e-mail address removed)" 'Not a valid email address
strSubject = "My Test Message with formatted fonts"
strCC = ""
strBCC = ""

strBody1 = "This is normal Times New Roman print in the body of the text"

strBody2 = "This is bold Times New Roman print in the body of the text" _

strBody3 = "This is normal blue Times New Roman print in the body of the text"

strBody4 = "This is bold blue Times New Roman print in the body of the text"

strBody5 = "This is normal Times New Roman print in the body of the text"

strBody6 = "This is normal Courier print in the body of the text"

lngErrNumber = 0 'Initialize to no error
On Error Resume Next
Set objOutlook = GetObject(, "Outlook.Application")
lngErrNumber = Err.Number
On Error GoTo 0 'Re-enable error trapping ASAP
If lngErrNumber > 0 Then
Set objOutlook = CreateObject("Outlook.Application")
Set objNameSpace = objOutlook.GetNamespace("MAPI")

'Following line not required if Outlook has saved logon details
'objNameSpace.Logon "Default Outlook Profile", , False, True

objNameSpace.GetDefaultFolder(olfolderinbox).Display
End If

Set objMail = objOutlook.CreateItem(olMailItem)

With objMail
.To = strTo
.CC = strCC
.BCC = strBCC
.Subject = strSubject
.BodyFormat = olFormatHTML
'Notes: Use <br> for line feeds.
'<b> is bold print.
'Prefix forward slash turns off the format.
'Do not include parameters to turn off fonts; _
Use only </font> because some web mails _
will print the parameters.
'Font size is NOT equivalent to Point size.
'Single quotes around font name (not required if no spaces)
.HTMLBody = "<font size=3 face='times new roman'>" & strBody1 &
"<br><br>" & _
"<b>" & strBody2 & "</b><br><br>" & _
"<font color=blue>" & strBody3 & "</font><br><br>" & _
"<b><font color=blue>" & strBody4 & "</font></b><br><br>" & _
strBody5 & "</font><br><br>" & _
"<font size=3 face=courier>" & strBody6 & "</font>"

If bolDisplaySend = True Then
.Display
'Ensure email is top window.
'Full Name of window can change depending on _
Tools -> Options -> Mail Format
'Changing this option for outgoing mail changes _
the window name.
'However, AppActivate appears NOT to require _
entire name but needs up to end of - Message _
which is included in heading irrespective of _
the format chosen.
AppActivate strSubject & " - Message"
Else
lngErrNumber = 0 'Initialize to no error
On Error Resume Next
.Send
lngErrNumber = Err.Number
On Error GoTo 0 'Re-enable error trapping ASAP
If lngErrNumber > 0 Then
'Ensures MsgBox not hidden under another window
AppActivate "form name"
MsgBox "Email was not sent." & vbCrLf & vbCrLf & _
"Can occur if User answers No to send on security popup"
End If
End If
End With

Set objMail = Nothing
Set objNameSpace = Nothing
'objOutlook.Quit 'Closes Outlook.
'Keeping open alleviates some problems.
Set objOutlook = Nothing
'Without the following code Outlook folder
'can remain on top of your Application window.
'Not used if using Display in lieu of Send.
If bolDisplaySend = False Then
'Bring your application or form to the top.
AppActivate "form name"
End If

End Sub
 
You can use CSS in the HTMLBody as pointed out by Daniel.

It works in Outlook, I've done it.

Funny thing though. Send such a message to yourself. As you indicated
it will look fine. Now forward it. In my experience a lot of the
styling is discarded in the forwarded message.
 
Back
Top