Sending HTML Email from Access Using a Command Button

melonwand

New Member
Joined
Sep 15, 2015
Messages
1
I have probably spent more time trying to figure this out by myself than I should have. I have an access database that we use to process orders. I know how to send emails as an attachment but what we need is to have an HTML email open with specific wording. Below is the code I have entered. Sometimes it sort of works but it opens an email and there is no formatting so the carriage returns are not working. Now I'm getting an error message compile syntax error. Any help would be greatly appreciated.

This is the code...
Rich (BB code):
Sub SendEmail()
     
      Dim oOutlook As Outlook.Application
      Dim oEmailItem As MailItem
      'Prevent 429 error, if outlook not open
10    On Error Resume Next
20    Err.Clear
30    Set oOutlook = GetObject(, "Outlook.Application")
40    If Err.Number <> 0 Then
50    Set oOutlook = New Outlook.Application
60    End If
      
70    Set db = CurrentDb()
80    Set recordset = CurrentData
90    Set MailList = db.OpenRecordset("Email_BO_Qry")
100   Set oEmailItem = oOutlook.CreateItem(olMailItem)
110   Set oOutlookMail = HTMLBody
120   With oEmailItem
130   .To = 
140   .Subject = "Dani Leather USA - Product Back Order"
150   .BodyFormat = olFormatHTML
160   .HTMLBody = _
170       "Thank you for your recent order.  Unfortunately, the following item(s) you've ordered are currently not in stock in the NJ warehouse and on order with the tannery." & vbCrLf & _
180       & vbCrLf & _(Color) & vbCrLf & _
190       "Below are a few suggestions we can offer for the back ordered item." & vbCrLf & _
200       & vbCrLf & _"1. We can place the item on back order and deliver your order as soon as we receive it." & vbCrLf & _
210       "2. If this is a stock item, we can check with the tannery for availablity. If it is available, we can have the leather airshipped directly to you (air freight charges may be applied)."& vbCrLf & _
220       "3. We could present you with an alternate similar color." & vbCrLf & _
230       & vbCrLf & _"If you have any questions or you'd like to make a change to your order, please call us. We appreciate your business, and we apologise for any inconvenience this delay causes you."
240   .Display
250   End With
260   Set oEmailItem = Nothing
270   Set oOutlook = Nothing




End Sub


Private Sub Command7_Click()
10    Call SendEmail
End Sub[/code]
 
Last edited by a moderator:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
HTML does not recognize &vbcrlf as a carriage return/linefeed character. This may be your syntax error as well. First, assign your body text to a string variable using the construct method you have, then assign the variable to the object member (.HTMLBody = svBodyText). Also, line 110 does not look right. I think you are assigning nothing to it at this point since the value of .HTMLBody does not get defined until line 160.

An alternative to my suggestion would be to use only HTML code characters to achieve your formatting, but I would use the first method.
I see that I cannot even include one here as an example - the forum removed the new paragraph character I tried to include in my post.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,581
Messages
6,131,541
Members
449,654
Latest member
andz

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top