{"id":53108,"date":"2023-06-25T16:13:08","date_gmt":"2023-06-25T15:13:08","guid":{"rendered":"https:\/\/ekiwi-blog.de\/53108\/vba-outlook-format-e-mail-as-html\/"},"modified":"2023-06-25T16:31:03","modified_gmt":"2023-06-25T15:31:03","slug":"vba-outlook-format-e-mail-as-html","status":"publish","type":"post","link":"https:\/\/ekiwi-blog.de\/en\/53108\/vba-outlook-format-e-mail-as-html\/","title":{"rendered":"VBA Outlook: Format e-mail as html"},"content":{"rendered":"<p>Anyone who does a lot of programming with the <abbr title=\"Visual Basic for Application\">VBA<\/abbr> editor in order to write a few macros that automate recurring tasks will quickly come into contact with the <em>MailItem<\/em>.<\/p>\n<p>The <em>MailItem<\/em> represents the object of the respective e-mail in the code editor. You can assign various properties to it. For example, you can <a href=\"https:\/\/ekiwi-blog.de\/en\/53092\/vba-outlook-mark-e-mail-as-read\/\">mark selected e-mails as read<\/a> with a VBA macro or write an automatic reply to an e-mail. One would then also like to assign the correct formatting, e.g. HTML, to such an automatic reply e-mail, as HTML offers considerably more design options than a pure text e-mail.<\/p>\n<h2>Use <em>BodyFormat<\/em> property<\/h2>\n<p>To achieve HTML formatting, one must use the <em>BodyFormat<\/em> property, as shown in the following image and code example.<\/p>\n<figure id=\"attachment_53099\" aria-describedby=\"caption-attachment-53099\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-53099\" src=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/06\/vba-outlook-email-als-html-formatieren.jpg\" alt=\"Screenshot VBA source code to format a MailItem as HTML mail\" width=\"650\" height=\"247\" srcset=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/06\/vba-outlook-email-als-html-formatieren.jpg 650w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/06\/vba-outlook-email-als-html-formatieren-300x114.jpg 300w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><figcaption id=\"caption-attachment-53099\" class=\"wp-caption-text\">Assign the HTML format to BodyFormat property<\/figcaption><\/figure>\n<pre><code><span style=\"color: #0000ff;\">Sub<\/span> ReplyMSG()\r\n    <span style=\"color: #0000ff;\">Dim<\/span> olItem <span style=\"color: #0000ff;\">As<\/span> Outlook.MailItem\r\n    <span style=\"color: #0000ff;\">Dim<\/span> olReply <span style=\"color: #0000ff;\">As<\/span> MailItem\r\n    \r\n    <span style=\"color: #0000ff;\">For Each<\/span> olItem <span style=\"color: #0000ff;\">In<\/span> Application.ActiveExplorer.Selection\r\n        <span style=\"color: #0000ff;\">Set<\/span> olReply = olItem.Reply\r\n        <mark>olReply.BodyFormat = olFormatHTML\r\n<\/mark> <span style=\"color: #008000;\">       '...more code here<\/span>\r\n    <span style=\"color: #0000ff;\">Next<\/span> olItem\r\n<span style=\"color: #0000ff;\">End Sub<\/span><\/code><\/pre>\n<p>In the sample code, reply mails are generated for all emails marked in the inbox and each <em>MailItem<\/em> of the reply mail is assigned the html format via the <strong>BodyFormat<\/strong> property (<strong><em>olFormatHTML<\/em><\/strong>).<\/p>\n<p><em>BodyFormat<\/em> is an enumeration, which means that you can also simply assign the appropriate numerical value. Besides <em>olFormatHTML<\/em> there are the following:<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li><em>olFormatUnspecified <\/em>(value 0): no specific formatting<\/li>\n<li><em>olFormatPlain <\/em>(value 1): the mail object is formatted as text-only<\/li>\n<li><em>olFormatHTML <\/em>(value 2): Mail is formatted as HTML<\/li>\n<li><em>olFormatRichText (value 3): <\/em>Mail is output in rich text format, which also offers design options like html.<em><br \/>\n<\/em><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>Note:<\/strong><\/p>\n<p>In order for the code execution to work properly in Outlook, the following must be set under <em>&#8220;Tools&#8221; \u27a4 &#8220;References&#8221;<\/em> the correct libraries must be selected.<\/p>\n<figure id=\"attachment_53075\" aria-describedby=\"caption-attachment-53075\" style=\"width: 445px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-53075 size-full\" src=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/06\/vba-editor-necessary-references-for-outlook.jpg\" alt=\"Screenshot of the necessary references in the VBA editor\" width=\"445\" height=\"364\" srcset=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/06\/vba-editor-necessary-references-for-outlook.jpg 445w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/06\/vba-editor-necessary-references-for-outlook-300x245.jpg 300w\" sizes=\"auto, (max-width: 445px) 100vw, 445px\" \/><figcaption id=\"caption-attachment-53075\" class=\"wp-caption-text\">Set the required library under Tools \u27a4 References<\/figcaption><\/figure>\n<p>Now you can continue working with <em>olReply<\/em> and assign text in the form of HTML code, for example.<\/p>","protected":false},"excerpt":{"rendered":"<p>Anyone who does a lot of programming with the VBA editor in order to write a few macros that automate<\/p>\n","protected":false},"author":2,"featured_media":6532,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1555],"tags":[2252,1707,2737,1857,1858,2168,1569,1861,2715,1862,2068],"class_list":["post-53108","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-en","tag-code-en","tag-macro","tag-macros","tag-microsoft-en-2","tag-microsoft-office-en-2","tag-office-en","tag-outlook-en","tag-vba-en-2","tag-vba-code-en","tag-visual-basic-for-application-en-2","tag-windows-en-2"],"_links":{"self":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts\/53108","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=53108"}],"version-history":[{"count":0,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts\/53108\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/media\/6532"}],"wp:attachment":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/media?parent=53108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/categories?post=53108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/tags?post=53108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}