HTML body in e-mail sent from access (1 Viewer)

mari_hitz

Registered User.
Local time
Yesterday, 16:32
Joined
Nov 12, 2010
Messages
120
Hi everybody,


Hope you can help me. I have a database to send e-mails from access trough Outlook. The issue is that I would like the body of the e-mail to have hyperlinks, colours, bolds and that I can get it from plain text.

This is the code I have:

Code:
Dim MyDB As Database
  Dim MyRS As Recordset
  Dim MyTable As Recordset
  Dim objOutlook As Outlook.Application
  Dim objOutlookMsg As Outlook.MailItem
  Dim objOutlookRecip As Outlook.Recipient
  Dim objOutlookAttach As Outlook.Attachment
  Dim TheAddress As String
  Dim TheBody As String
  Dim TheCopy As String
  
 

  Set MyDB = CurrentDb
  Set MyRS = MyDB.OpenRecordset("Sheet2")
  MyRS.MoveNext
  
  
  

  ' Create the Outlook session.
  Set objOutlook = CreateObject("Outlook.Application")
  
  Do Until MyRS.EOF
  ' Create the e-mail message.
  Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
  TheAddress = MyRS![Enterprise]
  TheBody = "This is a system-generated e-mail, please do not reply. For support please refer to:" & vbNewLine & _
     "Dear " & MyRS![First] & "," & "it has come to our attention that you have" & vbNewLine & _
   "Should you have another further inquiries, please do not hesitate to contact us" & vbNewLine & _
          " " & vbNewLine & _
          "People Mobility"

          
  TheCopy = MyRS![ProjectManager]

  
     With objOutlookMsg
        ' Add the To recipients to the e-mail message.
        Set objOutlookRecip = .Recipients.Add(TheAddress)
        objOutlookRecip.Type = olBCC

        
        ' Set the Subject, the Body, and the Importance of the e-mail message.
        .To = MyRS![Enterprise]
        .Subject = "Action Required: Please review assignment and/or MyTimeandExpenses information"
        .Body = TheBody
        .CC = TheCopy & ";" & "dario.a.tejero"
        .Importance = olImportanceHigh  'High importance
         


        
        
    
     
        ' Resolve the name of each Recipient.
        For Each objOutlookRecip In .Recipients
           objOutlookRecip.Resolve
           If Not objOutlookRecip.Resolve Then
             objOutlookMsg.Display
           End If
        Next
        .Send
      End With
      MyRS.MoveNext
   Loop
   Set objOutlookMsg = Nothing
   Set objOutlook = Nothing
   Set MyTable = Nothing

End Sub

How can I make the body to be HTML format, or, can I have a e-mail template storaged in my PC and then indicate to use it whenever the e-mail is sent? Please help me; I have tried to read it and look it over the internet but I need help doing this.

Thanks in advance,

Marina
 

Tallbloke

Registered User.
Local time
Today, 00:32
Joined
Aug 17, 2006
Messages
66
Code:
.HTMLBody = "<html><body><font face=calibri>Your Message Here</font></body></html>"
 

mari_hitz

Registered User.
Local time
Yesterday, 16:32
Joined
Nov 12, 2010
Messages
120
Thanks Tallbloke for your reply.

I am kindda new with HTML and access, I have always used plain text,could you please be so kind to guide me with this. Because you had put the code, however, the text I have to sent is pretty long and contains hyperlinks, as I said on my first post.
How do I add an hyperlink? How do I indicate to go to the next line?

I have created a htm document as a template in my PC and used to send the e-mail with the format I would like, however, the e-mail is not being sent.

Could you please elaborate a little bit more and help me? If you can not, I appreciate your help and your support :)

Kind regards,

Marina
 

mari_hitz

Registered User.
Local time
Yesterday, 16:32
Joined
Nov 12, 2010
Messages
120
Thanks Tallbloke for your help.

I could finally sent the e-mail as HTML, I have another issue now but I would really appreciate if someone could please help me.

I have created a table with the wording of my e-mail and since the data is set as rich text it keeps the hyperlinks and colors, etc. The data that I paste into this table is with font Calibri; however when the e-mail is sent it changes to times new roman. I have changed the font in the table. I have created a form for the table and indicating the code to go to the form, which I set the text box with Calibri, however the e-mail is sent with Times New Roman. Do you have any idea why this is happening? Is because I copy and paste the data to the table? Should I type it myself? Here is my code (I have tried to put the font code between "TheBody" part of .HTMLBody but it sends me the word "TheBody" instead of the body in font Calibri) :

Code:
Private Sub Command43_Click()
Dim MyDB As Database
  Dim MyRS As Recordset
  Dim MyForm As Form
  Dim objOutlook As Outlook.Application
  Dim objOutlookMsg As Outlook.MailItem
  Dim objOutlookRecip As Outlook.Recipient
  Dim objOutlookAttach As Outlook.Attachment
  Dim TheAddress As String
  Dim TheBody As String
  
   
  Set MyDB = CurrentDb
  Set MyRS = MyDB.OpenRecordset("Sheet2")
  MyRS.MoveFirst
  Set MyForm = Forms(Memo)
  
  ' Create the Outlook session.
  Set objOutlook = CreateObject("Outlook.Application")
  
  Do Until MyRS.EOF
  ' Create the e-mail message.
  Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
  TheAddress = MyRS![Enterprise]
  TheBody = Forms!Memo!Field1
  

     With objOutlookMsg
        ' Add the To recipients to the e-mail message.
        Set objOutlookRecip = .Recipients.Add(TheAddress)
        objOutlookRecip.Type = olBCC

        
        ' Set the Subject, the Body, and the Importance of the e-mail message.
        .To = MyRS![Enterprise]
        .Subject = "ACTION REQUIRED: Confirm Your Assignment Start Date"
        .HTMLBody = TheBody
        .Importance = olImportanceHigh  'High importance
        
        
         
        
     
        ' Resolve the name of each Recipient.
        For Each objOutlookRecip In .Recipients
           objOutlookRecip.Resolve
           If Not objOutlookRecip.Resolve Then
             objOutlookMsg.Display
           End If
        Next
        .Send
      End With
      MyRS.MoveNext
   Loop
   Set objOutlookMsg = Nothing
   Set objOutlook = Nothing
End Sub
 

arjun5381

Registered User.
Local time
Yesterday, 16:32
Joined
May 10, 2016
Messages
32
Hi, i am getting an compiler error "User-Defined type not Defined" on below code
Dim objOutlook As Outlook.Application

can someone please help me.
 

isladogs

MVP / VIP
Local time
Today, 00:32
Joined
Jan 14, 2017
Messages
18,209
You need to add the Microsoft Outlook reference in the VBE.
Or change the code so it uses late binding

BTW if that doesn't solve it you should start your own thread rather than hijack an old one from 2012.
 

Wayne

Crazy Canuck
Local time
Yesterday, 19:32
Joined
Nov 4, 2012
Messages
176
What is the actual "body" of the message you are trying to send ? Please send an example of the text you are looking to see in the body of the message on the email output (including any hyperlinks you want to include).

Wayne
 

Users who are viewing this thread

Top Bottom