{"id":62449,"date":"2024-02-04T17:29:26","date_gmt":"2024-02-04T16:29:26","guid":{"rendered":"https:\/\/ekiwi-blog.de\/62449\/vba-outlook-setchange-font-and-font-size\/"},"modified":"2024-02-04T17:36:56","modified_gmt":"2024-02-04T16:36:56","slug":"vba-outlook-setchange-font-and-font-size","status":"publish","type":"post","link":"https:\/\/ekiwi-blog.de\/en\/62449\/vba-outlook-setchange-font-and-font-size\/","title":{"rendered":"VBA Outlook: Set or change font and font size of an email"},"content":{"rendered":"<p><abbr title=\"Visual Basic for Application\">VBA<\/abbr> can be used for wonderful work simplifications in the Microsoft Office world and one can only hope that Microsoft never abolishes VBA. Let&#8217;s say you&#8217;ve created a VBA script to automatically generate an email or automatically reply to an email. Of course, in such a case, you want to format the e-mail a little according to your wishes and, for example, change the font size or font type. There are two options available here. This depends on whether you want your email to be formatted in HTML format or in rich text format.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of content<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ekiwi-blog.de\/en\/62449\/vba-outlook-setchange-font-and-font-size\/#Setting_the_font_and_font_size_using_HTML\" >Setting the font and font size using HTML<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ekiwi-blog.de\/en\/62449\/vba-outlook-setchange-font-and-font-size\/#Adjust_font_and_font_size_in_rich_text_format\" >Adjust font and font size in rich text format<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Setting_the_font_and_font_size_using_HTML\"><\/span>Setting the font and font size using HTML<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The script for <a href=\"https:\/\/ekiwi-blog.de\/en\/62792\/vba-outlook-create-new-e-mail\/\">creating a new email<\/a> looks like this. Make sure that <em>olFormatPlain<\/em> is not assigned to this property, but <em>olFormatHTML<\/em>. In this case, the mail is formatted using HTML, which provides maximum freedom in the design of the text.<\/p>\n<pre><code><span style=\"color: #3366ff;\">Public Sub<\/span> NewMailWithSpecificFontSize()\r\n    <span style=\"color: #3366ff;\">Dim<\/span> olItem <span style=\"color: #3366ff;\">As<\/span> Outlook.MailItem\r\n   \r\n    <span style=\"color: #3366ff;\">Set<\/span> olItem = Application.CreateItem(olMailItem)\r\n    \r\n    <span style=\"color: #3366ff;\">With<\/span> olItem\r\n         .to = \"john.doe@my-domain.de\"\r\n         .subject = \"In urgent matter\"\r\n         .BodyFormat = olFormatHTML\r\n         .HTMLBody = \"&lt;BODY style=<strong>font-size:20pt<\/strong>;<strong>font-family:Calibri<\/strong>&gt;&lt;p&gt;Hello,&lt;\/p&gt;&lt;p&gt;I am contacting you on urgent matter XY!&lt;\/p&gt;&lt;p&gt;Yours sincerely,&lt;br&gt;Frederike van Sanghausen&lt;\/p&gt;&lt;\/BODY&gt;\"\r\n         .display\r\n    <span style=\"color: #3366ff;\">End With<\/span>\r\n\r\n    <span style=\"color: #3366ff;\">Set<\/span> olItem = <span style=\"color: #3366ff;\">Nothing<\/span>\r\n<span style=\"color: #3366ff;\">End Sub<\/span><\/code><\/pre>\n<p>In this case, you can add CSS styles to automatically change the font and font size. In the code example above, the font size within the HTML text was set to <strong>20pt<\/strong>, which is quite large.<br \/>\n<strong>Important<\/strong>: The text must not be assigned to the <strong><em>Body<\/em><\/strong> property, otherwise you will see the HTML code in the email itself. The HTML code must be assigned to the <strong><em>HTMLBody<\/em><\/strong> property.<\/p>\n<p>If you execute the macro, the email will look like this:<\/p>\n<figure id=\"attachment_62440\" aria-describedby=\"caption-attachment-62440\" style=\"width: 540px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-62440 size-full\" title=\"VBA-Outlook: customising the font and font size of an email with HTML\" src=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2024\/02\/vba-email-email-adjust-font-and-font-size-with-html-format.jpg\" alt=\"Screenshot of Outlook email created with VBA macro in HTML format. The font and font size properties were set.\" width=\"540\" height=\"504\" srcset=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2024\/02\/vba-email-email-adjust-font-and-font-size-with-html-format.jpg 540w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2024\/02\/vba-email-email-adjust-font-and-font-size-with-html-format-300x280.jpg 300w\" sizes=\"auto, (max-width: 540px) 100vw, 540px\" \/><figcaption id=\"caption-attachment-62440\" class=\"wp-caption-text\">Font and font size set via HTML\/CSS style code<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Adjust_font_and_font_size_in_rich_text_format\"><\/span>Adjust font and font size in rich text format<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Outlook also offers the alternative option of formatting emails as rich text. Here you can also customise the font and font size as you wish. The VBA code for this looks like this:<\/p>\n<pre><code><span style=\"color: #3366ff;\">Public Sub<\/span> NewMailWithSpecificFontSize2()\r\n    <span style=\"color: #3366ff;\">Dim<\/span> olItem <span style=\"color: #3366ff;\">As<\/span> Outlook.MailItem\r\n    <span style=\"color: #3366ff;\">Dim<\/span> wdApp <span style=\"color: #3366ff;\">As<\/span> Object\r\n    <span style=\"color: #3366ff;\">Dim<\/span> wdDoc <span style=\"color: #3366ff;\">As<\/span> Object\r\n    <span style=\"color: #3366ff;\">Dim<\/span> wdRange <span style=\"color: #3366ff;\">As<\/span> Object\r\n   \r\n    <span style=\"color: #3366ff;\">Set<\/span> olItem = Application.CreateItem(olMailItem)\r\n    \r\n    <span style=\"color: #3366ff;\">With<\/span> olItem\r\n         .to = \"john.doe@my-domain.de\"\r\n         .subject = \"In urgent matter\"\r\n         .bodyFormat = olFormatRichText\r\n         .body = \"Hello,\" &amp; vbCrLf &amp; vbCrLf &amp; \"I am contacting you on urgent matter XY!\" &amp; vbCrLf &amp; vbCrLf &amp; \"Yours sincerely,\" &amp; vbCrLf &amp; \"Frederike van Sanghausen\"\r\n         \r\n         <span style=\"color: #3366ff;\">Set<\/span> wdApp = .GetInspector\r\n         <span style=\"color: #3366ff;\">Set<\/span> wdDoc = wdApp.WordEditor\r\n         <span style=\"color: #3366ff;\">Set<\/span> wdRange = wdDoc.Range\r\n         wdRange.WholeStory\r\n         <span style=\"color: #3366ff;\">With<\/span> wdRange\r\n            .Font.Name = \"Arial Rounded MT Bold\"\r\n            .font.size = 16.5\r\n         <span style=\"color: #3366ff;\">End With<\/span>\r\n         \r\n         .display\r\n   <span style=\"color: #3366ff;\"> End With<\/span>\r\n\r\n    <span style=\"color: #3366ff;\">Set<\/span> olItem = <span style=\"color: #3366ff;\">Nothing<\/span>\r\n<span style=\"color: #3366ff;\">End Sub<\/span><\/code><\/pre>\n<p>You can see that you have to create a Word document object (<em>wdDoc<\/em>) to be able to apply the rich text formatting. Only then do you have access to the properties <em>Font.Name<\/em> and <em>Font.Size<\/em>. The email object (<em>olItem<\/em>) itself does not offer these properties.<\/p>\n<p>The result then looks like the image below:<\/p>\n<figure id=\"attachment_62444\" aria-describedby=\"caption-attachment-62444\" style=\"width: 534px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-62444 size-full\" title=\"VBA-Outlook: Font and font size of an email set with Rich-Text Word Editor\" src=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2024\/02\/vba-email-font-fontsize-rich-text-format.jpg\" alt=\"Screenshot of the automatically generated email in which the font size and font have been automatically adjusted using the rich text format\" width=\"534\" height=\"491\" srcset=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2024\/02\/vba-email-font-fontsize-rich-text-format.jpg 534w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2024\/02\/vba-email-font-fontsize-rich-text-format-300x276.jpg 300w\" sizes=\"auto, (max-width: 534px) 100vw, 534px\" \/><figcaption id=\"caption-attachment-62444\" class=\"wp-caption-text\">Macro generates e-mail in rich text format with set font and font size<\/figcaption><\/figure>\n<p>If you use the <em>olFormatPlain <\/em>as formatting, the email is formatted as a plain text document. In this case, it is not possible to format the text of the email. But this can also be sufficient in some cases.<\/p>","protected":false},"excerpt":{"rendered":"<p>VBA can be used for wonderful work simplifications in the Microsoft Office world and one can only hope that Microsoft<\/p>\n","protected":false},"author":2,"featured_media":13630,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1555],"tags":[3156,3155,2252,3035,1707,2737,3154,1703,1704,1858,2168,1569,3231,1700,1705,2715,1706,1558],"class_list":["post-62449","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-en","tag-automatisch-en","tag-automatisieren-en","tag-code-en","tag-e-mail-en","tag-macro","tag-macros","tag-makros-en","tag-microsoft-en","tag-microsoft-office-en","tag-microsoft-office-en-2","tag-office-en","tag-outlook-en","tag-powerpoint-en","tag-programming","tag-vba-en","tag-vba-code-en","tag-visual-basic-for-application-en","tag-windows-en"],"_links":{"self":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts\/62449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/comments?post=62449"}],"version-history":[{"count":0,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts\/62449\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/media\/13630"}],"wp:attachment":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/media?parent=62449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/categories?post=62449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/tags?post=62449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}