Forum

VBA to create new e...
 
Notifications
Clear all

VBA to create new email in Outlook using different font, font size & colour

10 Posts
4 Users
0 Reactions
855 Views
(@yola-rego)
Posts: 15
Active Member
Topic starter
 

Hello All and Happy 2018!

Can I please have a code to create a new e-mail message, contact, or calendar invite, the format will be in HTML, and use font - Times Roman, font size - 11, and font colour - blue. The code should also work when replying, forwarding, accepting etc. For the latter (responding/forwarding/accepting) I don’t mind if this is a separate code, though ideally would like it all to be in one.

I use Outlook 2013.

Thank you.

ER

 
Posted : 17/01/2018 7:03 pm
(@sunnykow)
Posts: 1417
Noble Member
 

Why do you need codes when you can change the setting in Outlook itself?

(Outlook 2010) File - Options - Mail - Stationery and Fonts...

 
Posted : 17/01/2018 11:53 pm
(@yola-rego)
Posts: 15
Active Member
Topic starter
 

Hello SunnyKow,

Thank you for your suggestion.

I know about the Options feature, and the ability to change formatting from there. The code is needed as there is a default setting that is returns the original format settings. I have to change the Options with each log in (several times a day) or manually with each message/invite/contact (new, forward, reply, etc). A code is more efficient, and hopefully someone can please submit one for me? PLEASE!

Thanks for taking the time to suggest.

 

ER

 
Posted : 18/01/2018 11:02 am
Anders Sehlstedt
(@sehlsan)
Posts: 968
Prominent Member
 

Hello,

I suggest that you take a look at this blog post and then adapt the code you see being used to your need. It is a good way to learn new things. Good luck!

Br,

Anders Sehlstedt

 
Posted : 18/01/2018 6:34 pm
(@yola-rego)
Posts: 15
Active Member
Topic starter
 

Hi Anders,

Thank you for the suggested link to practice and create my own script. I came up with the one below which creates a new HTML email message but does not override the default font/size/colour.

Can you please see where it needs editing?

Thanks

ER

Sub createnewmessage()

'Create an Outlook object and new mail message

Set OutlookApp = CreateObject("Outlook.Application")

Set OutlookMail = OutlookApp.CreateItem(0)

 

'Display email and specify To, Subject, etc

With OutlookMail

 

.Display

.To = Email_To

.CC = Email_CC

.BCC = Email_BCC

.Subject = EmailSubject & CurrentMonth

Dim objMail As MailItem

On Error Resume Next

 

Set objItem = Application.ActiveInspector.CurrentItem

If Not objItem Is Nothing Then

    If objItem.Class = olMail Then

        Set objMail = objItem

        .HTMLBody = .HTMLBody

        Dim StrAgnt As String

StrAgnt = "<HTML><H2>The body HTML.</H2><BODY>Hi. </BODY></HTML>" & vbCrLf & vbCrLf & _

    Selection.WholeStory

    Selection.Copy

    Selection.WholeStory

    Selection.Font.Name = "Tahoma"

    Selection.Font.Size = 11

    Selection.PasteAndFormat (wdFormatSurroundingFormattingWithEmphasis)

    Selection.Delete Unit:=wdCharacter, Count:=1

    End If

End If

 End With

End Sub

 
Posted : 23/01/2018 11:58 am
(@yola-rego)
Posts: 15
Active Member
Topic starter
 

Hi again,

Could someone please check the code and offer a solution? It does not update the font.

Thank you,

ERCry

 
Posted : 24/01/2018 6:29 pm
Anders Sehlstedt
(@sehlsan)
Posts: 968
Prominent Member
 

Hi,

As I don't write VBA myself I am not really correct person to give advice, but I do know enough to be able to understand what the code is supposed to do. You are referencing to the .HTMLBody property, so should you then not use HTML to format the text? Either change to use .RTFBody property and see if it works with the existing formatting code or use HTML code to format the text in the body section. If you use .RTFBody then I suppose you need to remove the HTML tags in your StrAgnt.

I checked the MSDN pages for more information about syntax. In the example there they use a different object declaration. Do try their approach.

Br,

Anders

 
Posted : 25/01/2018 6:58 pm
(@yola-rego)
Posts: 15
Active Member
Topic starter
 

nothing worked CryCry

Perhaps because I really am not experienced in any VBA, and should really know how to even read code.

Is there anyone who can help me, please?

 
Posted : 27/01/2018 11:18 am
(@catalinb)
Posts: 1937
Member Admin
 

Hi ER,

All you have to do is to use HTML formats, to set up the message body font, color and other attributes. The following text will work in VB editor:

.HTMLBody = "

" & "Text 1" & "

" & vbNewLine & _
            "

" & "Text 2" & "

" & vbNewLine & _
            "

" & "Text 3" & "

"

 

See more at: html-text-style-attributes

You can play around with formattings here:

http://htmledit.squarefree.com

Note that "

" & "Text 1" & "

" & vbNewLine cannot be used as is in HTML editor, replace the pairs of double quotes with one double quote, and remove single quotes:

Text 1

Later Edit:

Browsers will not display correctly the formatted text, see attached text file for a correct text sample.

 
Posted : 28/01/2018 4:32 am
(@yola-rego)
Posts: 15
Active Member
Topic starter
 

Thank you Catalin.

I will give it a try and get back to you.

ER

 
Posted : 02/02/2018 12:44 pm
Share: