Vba outlook html body

Содержание
  1. Contact US
  2. Posting Guidelines
  3. How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.
  4. How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.
  5. How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.
  6. RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.
  7. RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.
  8. RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.
  9. RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.
  10. CODE
  11. RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.
  12. RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.
  13. RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.
  14. CODE
  15. RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.
  16. RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.
  17. RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.
  18. RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.
  19. RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.
  20. RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.
  21. RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.
  22. RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.
  23. RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.
  24. CODE
  25. RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.
  26. RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.
  27. Red Flag Submitted
  28. Reply To This Thread
  29. Posting in the Tek-Tips forums is a member-only feature.
  30. Send a formatted email using VBA and Outlook
  31. Using the Outlook MailItem
  32. Make a formatted body with customized salutation
  33. Writing body code
Читайте также:  Задача математического моделирования python

Contact US

*Tek-Tips’s functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.

How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.

How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.

I am building emails in VBA and would like to define the overall font for the body of the email. I would also provide text that would actually be a hyperlink, such as: go to this our SharePoint site and it would go to that site, or People names that would be a link. I currently have the text formatted in a .oft outlook html template but when it displays no formatting is maintained. I would rather code it in the .Body of the VBA code if I knew how. I will go ahead and add any attachments when I need a lot of formatting.

RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.

RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.

What I have done in this situation is — create a Word document, format it the way you want with all hyperlinks, fonts, etc., and save it as HTML file. Then open the file in Notepad and see how it is done.
You could also open this HTML file in IE and go to View — Source and see the same information.
Just pick what you want and recreate it in your VBA code. It is just a text with some tags.

Читайте также:  Cast object to arraylist in java

There is a great need for a sarcasm font.

RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.

If I am using the .oft template does the .HTMLBody need to be included in the VBA? What am I missing that is causing the formatted template from dropping the bolds and causing the actual URL hyperlinks to display

If I decide not to use the .oft template I’m not sure how to code the .HTMLBody = «value» or variable(s) value. Can you give me some examples? This would also resolve another post that I have and will give you that number once I am out of this screen.

RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.

How about something simple:

CODE

Dim strBody As String strBody = " " strBody = strBody & "Click here to go to Google

" strBody = strBody & " " With YourMessage . .HTMLBody = strBody . End With

There is a great need for a sarcasm font.

RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.

As far as examples, I am trying:

«Hi » & «,

» & _
«THIS IS AN ALERT» & _ how to bold or change font?
«Please contact HR Services» & _ how to underline HR Services and make it a hyperlink (

RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.

We’d probably need to see your code to see how you are building your emails and how exactly you are using the oft

RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.

strongm is right, your code here would be beneficial, big time.

CODE

strBody = " Hi

" strBody = strBody & "THIS IS AN ALERT
" strBody = strBody & "Please contact HR Services

" strBody = strBody & " "

THIS IS AN ALERT
Please contact HR Services

There is a great need for a sarcasm font.

RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.

I’ll work on getting my code together because I’d like to show how I bring in the template which would be the ultimate because I would not need an attachment with the format I want for instructions, I could have those in the email itself!

RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.

RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.

Actually, a quick review (I set up a quick HTML-based OFT) and then used VBA to send it to myself — and encountered the same problems that you describe) suggests that there is a ‘feature’ regarding using a template to create an HTML mail — it appears expect to be .Dislay(ed) before sending, to ensure format is properly synchronised, but you probably don’t want to do that.

So I had a little think … how else can we trigger internal synchronisation? Well, presumably setting HTMLBody does — so I tried .HTMLbody = .HTMLbody before .Send … and it worked.

So, if using a template — make sure you force synchronisation before .Send(ing) with .HTMLBody = .HTMLBody.

And if handcrafting your HTML, stick it in .HTMLBody, not .Body

RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.

Wow that sounds promising,let me try. I got the same error on the http highlighted end of statement error, like I need something set to reference http? Let me try what your suggestion strongm.

RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.

This is perfect! Amazing 1 line of code to resolve bringing the template format .HTMLBody = .HTMLBody
strongm, if you want to put that same suggestion in Post: thread705-1789870: sending a formatted email in Access 2016 VBA using Outlook Template, I’ll give you a star there also, I wasn’t getting any hits there!

Thanks again and also to Andy for the HTML examples, I’ll use them another time!

RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.

RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.

Ooops. My fault.
There are double quotes in line «Please contact. «

There is a great need for a sarcasm font.

RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.

I got past the error but I am not seeing where any of the text is a hyperlink. Can you help me understand how a text value becomes a hyperlink?

RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.

Sure.
Start new Access. Have one form with one button
Add a Reference to Microsoft CDO for Windows 2000 Library
Paste this code into your _Click event for your button:

CODE

Dim objMessage As New CDO.Message Dim strBody As String strBody = " Hi

" strBody = strBody & "THIS IS AN ALERT
" strBody = strBody & "Please contact HR Services

" strBody = strBody & " " With objMessage .From color:red;">YourEMail@domain.com" .To color:red;">YourEMail@domain.com" .Subject = "This is a test alert" .HTMLBody = strBody With .Configuration.Fields .Item(CDO.cdoSMTPServer) color:blue;">YourSMTPServer" .Item(CDO.cdoSMTPServerPort) = 25 .Item(CDO.cdoSendUsingMethod) = CDO.cdoSendUsingPort .Item(cdoSMTPConnectionTimeout) = 10 .Update End With .Send End With Set objMessage = Nothing

Replace the RED text with your e-mail address, and BLUE text with your SMTPServer address.

Run this code and you should get an e-mail with the text as in my post from 27 Sep 18 14:00

There is a great need for a sarcasm font.

RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.

A text HTML link with displayable link text is defined as:

for which Andrzejek provided the following concrete example:

RE: How to code in VBA a HTML body of email, fonts, hyperlinks for text fields.

Of course it was my typing! Can’t copy to work machine. Left off the = after ‘a href’!
Much appreciated!

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Copyright © 1998-2023 engineering.com, Inc. All rights reserved.
Unauthorized reproduction or linking forbidden without expressed written permission. Registration on or use of this site constitutes acceptance of our Privacy Policy.

Источник

Send a formatted email using VBA and Outlook

Using HTML for your email content makes it much more expressive, including the ability to use fonts and bold, hyperlinks and even images. This article shows how to code a VBA macro to send a formatted email using VBA and Outlook. To make the email and the HTML content for its body fragments and code are inserted by Code VBA add-in. The HTML is created using a combination of fixed tag strings and class HTMLElement (automatically inserted in the project by the add-in when using fragments that rely on it)

In a module, create an empty Sub ‘HtmlMailDemo’ for the macro

Using the Outlook MailItem

We want to send an email to each recipient. To achieve this we make an empty line before .MoveNext to create the email. Now we will use the Code VBA add-in to connect to Outlook and create the mail: down the Code VBA menu, select Outlook followed by Create.

menu Create Outlook MailItem

This opens a Fragment Builder dialog to specify the use of the MailItem object:

dialog to specify the use of the MailItem object

  • Enter text for subject line;
  • Agree to have a new String variables strBody and strTo created to set the .To and .Body properties;
  • Accept the default value .Display for the MailItem’s Action setting. You will probably change this to change this to .Send once things are woking to your satisfaction.

After OK, the following code block is inserted

Dim mimEmail As Outlook.MailItem Dim appOutlook As Outlook.Application: Set appOutlook = New Outlook.Application Dim strBody As String: strBody = Set mimEmail = appOutlook.CreateItem(olMailItem) With mimEmail .To = strTo .Subject = "Taskforce meeting" '.Body = strBody 'use .HTMLBody in case of HTML!' .HTMLBody = strBody .Display End With

Here, we want a formatted body (using HTML), so we change .Body to .HTMLBody .

Make a formatted body with customized salutation

We now need to make suitable HTML for the body. After putting the cursor behind strBody = (part after : moved to the next line), from the Code VBA menu, we now select Outlook » HTML Body » _HTMLEmail » strBody . This inserts the following boilerplate HTML and adds a new class HTMLElement to your body.

Dim html As New HTMLElement With html .Tag = "html" .AddAttribute "lang", "en" Dim head As New HTMLElement With head .Tag = "head" 'see Setting the Viewport on https://www.w3schools.com/tags/tag_meta.asp 'see https://www.w3schools.com/tags/tag_style.asp .AppendContentValue "" & vbNewLine & _ "" & vbNewLine & _ "" End With Dim body As New HTMLElement With body .Tag = "body" .EndWithNewLine = True 'continue here defining the (HTML)Elements that build the content 'and add them using .AppendContentElement (and/or .AppendContentValue) End With .AppendContentElement head .AppendContentElement body strBody & vbNewLine & .Text End With

Dissecting the body HTML code creation:

  • First, HTMLElement html is created with the attribute lang=»en»;
  • Nested herein, the standard head and body elements are built up, which are at the end added using .AppendContentElement head and .AppendContentElement body statements;
  • The head element in this case is considered a boilerplate set of metatags and is added as a fixed string using .AppendContentValue ;
  • The body element obviously is the interesting part which is added before this element’s End With and is elaborated below;
  • Rounding up, the body code is obtained from the HTMLElement html using the .Text property and assigned to the body variable: strBody & vbNewLine & .Text

Writing body code

Follow the link to learn more about Code VBA tools for creating body HTML.

You can now run the macro (F5). The emails are opened in draft. If you extended this code to your satisfaction and all’s good, change .Draft to .Send for future enjoy.

automated customized emails

Sub HtmlMailDemo() Dim strEmailAddess As String Dim strFirstName As String Dim strLastName As String Dim appOutlook As Outlook.Application: Set appOutlook = New Outlook.Application strEmailAddess /cdn-cgi/l/email-protection" data-cfemail="f581db948d909bb5879c8398db9b99">[email protected]" strFirstName = "Thomas" strLastName = "Axen" Dim mimEmail As Outlook.MailItem Dim strBody As String Dim html As New HTMLElement With html .Tag = "html" .AddAttribute "lang", "en" Dim head As New HTMLElement With head .Tag = "head" 'see Setting the Viewport on https://www.w3schools.com/tags/tag_meta.asp 'see https://www.w3schools.com/tags/tag_style.asp .AppendContentValue "" & vbNewLine & _ "" & vbNewLine & _ "" End With Dim body As New HTMLElement With body .Tag = "body" .EndWithNewLine = True 'continue here defining the (HTML)Elements that build the content 'and add them using .AppendContentElement (and/or .AppendContentValue) End With .AppendContentElement head .AppendContentElement body strBody & vbNewLine & .Text End With Set mimEmail = appOutlook.CreateItem(olMailItem) With mimEmail .Subject = "Taskforce meeting" '.Body = strBody 'use .HTMLBody in case of HTML!' .HTMLBody = strBody .Display End With End Sub

CODE VBA — 4TOPS Copyright 1997-2022

Источник

Оцените статью