Search code examples
htmlexcelvbaoutlook

Concatenate a variable string on the same line of Html email body , Outlook VBA


I need to concatenate variable string str on the same line of email body (Line1).
I tried to add inside the string like that &str , But the output is just &str and not the values of str.
Also If I added after </p>" ,then it adds values of str on the next line.
Kindly How to fix this issue ?

Sub Send_Email()
 
    Dim objOutlookApp As New Outlook.Application
 
    Dim myEmail As Outlook.MailItem
     Set myEmail = objOutlookApp.CreateItem(olMailItem)
      myEmail.BodyFormat = olFormatHTML
       myEmail.Display
'___________________
 
    Dim str As String: str = "My Text"
 
    Dim Strbody As String
 
    Dim Style As String: Style = "<p style=font-size:12.5pt;font-family:Georgia>"
    Dim Line1 As String: Line1 = "&emsp;&emsp; Attached file contains </p>" & str  'this add str on the next line
 
    Strbody = Style & Line1
 
    myEmail.HTMLBody = Strbody & myEmail.HTMLBody
 
End Sub

Solution

  • The html tag p is a paragraph. If you want to include a string variable into a paragraph, it should be located before the ending </p> tag.

    Dim Style As String: Style = "<p style=font-size:12.5pt;font-family:Georgia>"
    Dim Line1 As String: Line1 = "&emsp;&emsp; Attached file contains " & str & "</p>"