Formatting a message for html email body

Kemidan2014

Board Regular
Joined
Apr 4, 2022
Messages
226
Office Version
  1. 365
Platform
  1. Windows
I keep getting a compile error when i added some font formating conditions to my msg string. I have tried different examples i have seen from google search. I believe its probably something as simple as I am not putting quotes, parenthesis or & in the correct spots.
1694615890147.png

I have a simple message I constructed for an email that we want to send out through Access.

All i want to do is just have the font size be 22pt in the body of the email

This is my original String set up. this worked but puts it in my emails default font size and font type.
Code:
msg = "Team," & ""<br><br>" & "The following issue needs to be corrected:" & "<br><br>" & _
      Me.[Program] & " " & Me.[Product] & " from Line"  & Me.[Line #] & " " & Me.ProcessID & " has a " & Me.[Abnormal Condition] & " abnormal condition." & "<br><br>" & _
       "Target should be "& Me.[Tolerance] & " Actual result was " & Me.[Actual] & "."  'define email message

Below is the current code where i attempted to add Font size format. but the bottom 2 lines go red and give me the compile error.

Code:
Private Sub Command258_Click()
Dim O As Outlook.Application 'Declare outlook object
Dim M As Outlook.MailItem 'Declare mail object
Dim db As DAO.Database 'declare database variable
Dim rst As DAO.Recordset ' declare recordset variable
Dim address As String 'declare string name for email
Set db = CurrentDb 'set the database
Set rst = CurrentDb.OpenRecordset("Select Email From emaillist Where Hotzone = '" & Me.hotzone & "'") 'set recordset for email recipients with SQL query

Dim msg As String 'declare variable is a string statement for email message

msg = "<body style=Font-size:22pt> Team, & <br><br> & The following issue needs to be corrected: & <br><br> & _"
      "<p style=font-size:22px> & Me.[Program] &   & Me.[Product] &  from Line  & Me.[Line #] &   & Me.ProcessID &  has a  & Me.[Abnormal Condition] &  abnormal condition & <br><br> </p>" & _
       "<p style=font-size:22px> Target should be  & Me.[Tolerance] &  Actual result was  & Me.[Actual] & .</p></Body>"  'define email message

Set O = New Outlook.Application 'set the outlook object
Set M = O.CreateItem(olMailItem) 'set the mail item

'Procedure to generate a multirecipient email string
With rst
 Do While Not .EOF
  'If rst!hotzone = Me.hotzone Then
  address = address & rst!Email & ";"
  rst.MoveNext
  'End If
 Loop
End With
  Debug.Print address
'procedure to create the email and send or display it
 With M
    .To = address
    .BodyFormat = olFormatHTML
    .HTMLBody = msg
    .Subject = "Attention Out of Spec Condition " & Me.[Program] & " " & Me.[Product]
    '.Send
    .Display
    
 End With
 
 'MsgBox "E-mail has been sent!", vbOKOnly
Set rst = Nothing 'empty the memory
Set M = Nothing
Set O = Nothing

End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
The latter code is missing alot of quotation marks. It also isn't concatenating the string with & _
Everything in the quotation marks comprises the HTML code. Everything outside of the quotation marks is part of the VBA code.

This should explain it: Strings in VBA
 
Upvote 0
I reviewed your link, thank for that reference but it excludes how you format using fields example "Me.[fieldname]". after adding all the quotations i originally had back in just shrinks the blue highlightling that displays when the compile error happens to the what it sees as the an ending "
 
Upvote 0
Start Debugging your strings with Debug.Print
I prefer to use Msg = Msg & " This is the next line" rather than continuation lines, plus I always make sure a space is at the start of that string instead of having to scroll to the far right all the time. :(
 
Upvote 0
If i have the compile error on the string itself due to syntax then it wont show debug.print
 
Upvote 0
If i have the compile error on the string itself due to syntax then it wont show debug.print
It is a string, so you should be able to see it and you need to debug.pront it beforetring to ise it. Comment out it's use anyway, until you get it correct.
Build it up, line by line, but you need a hell of a lot of " added to start getting anywhere.
 
Upvote 0
I did not know you could do that, is this something I activate by "stepping into"? to read it in the immediate window?

also Dan was correct I was not using " " in the correct locations here is the how i have it formatted now that actually worked and put the font size where I wanted it. i also had my : and = flip flopped.

VBA Code:
msg = "<body style=""Font-size:22px""> Team, <br><br> The following issue needs to be corrected: <br><br>" & _
         Me.[Program] & "  " & Me.[Product] & " from Line " & Me.[Line #] & "  " & Me.ProcessID & " has a " & Me.[Abnormal Condition] & " abnormal condition <br><br>" & _
       "Target should be " & Me.[Tolerance] & " Actual result was " & Me.[Actual] & ".</Body>"  'define email message
 
Upvote 0
Yes, use breakpoints and F8 to walk your code and inspect variables/fields by hovering the cursor over them and see the flow of the code, not how you think it is working, which mightbethe same, but might not.
 
Upvote 0
Test message to see if signature works.
And it does not. :(

Edit: Just not showing on Chrome for Android. I had added it via that as my laptop had a huge Windows update and unable to get on it.
Apologies for not using the test board forum.
 
Last edited:
Upvote 0
Please use the test board for testing. That's what it's for ;)
Also your signature is showing fine.
 
Upvote 0

Forum statistics

Threads
1,215,068
Messages
6,122,950
Members
449,095
Latest member
nmaske

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