Sagar
Sagar

Reputation: 13

Formatting specific part of text string in VBA

I am in process of creating a macro that will save the current workbook, create a new outlook message and attach the file to the message. My macro does that but I can not format the text in the body of the email to my liking.

Dim OutApp As Object
Dim OutMail As Object
Dim sBody, Customer As String

ActiveWorkbook.Save


sBody = "All," & Chr(10) & Chr(10) & "Please Approve attached Request below for " & rType & "." _
 & Chr(10) & Chr(10) & "Customer: " & customer & Chr(10)

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
    With OutMail
        .to = recip
        .CC = CCed
        .BCC = ""
        .subject = subject
        .Body = sBody
        .Attachments.Add ActiveWorkbook.FullName
        .display

       End With
    On Error GoTo 0

End Sub  

I want the following message to be displayed (with the format) in the email.

All,

Please Approve attached Request below for "rtype".

Customer: Stackoverflow

So, the word "customer" needs to be bold. I have tired multiple solutions but they do not work as this is creating an outlook mail object.

Any Help will be appreciated.

**

Solution: To make the HTML tags work change the body type to html by ".HTMLBody". and you will be able to use HTML Tags. Kudos to Dick Kusleika

**

Upvotes: 0

Views: 1300

Answers (2)

Dick Kusleika
Dick Kusleika

Reputation: 33145

HTML tags do work. I don't know why you say they don't.

sBody = "All,<br /><br />Please Approve attached request for " & rType & ".<br /><br /><strong>Customer:</strong> " & customer & "<br />"

then instead of the .Body property, use .HTMLBody

.HTMLBody = sBody

Upvotes: 1

99moorem
99moorem

Reputation: 1983

you have a few options

1)use HTML like a few people have commented

2)put that text on a hidden sheet and format it as required then ref it to the body as a range e.g. .Body = sheets("hidden_Body").range("A1:B10")

3)of you can try using something like below (please note below is used for adding one wingding character into a string and would need to be modified to fit your purpose)

Sub Build_Wingdings(Sh As Worksheet, rng As Range)

Dim cur_L As Integer
cur_L = 1
Sheets("Word_Specifications").Range("BZ9").Copy
Sh.Range(rng.Address).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

Application.CutCopyMode = False

With Sheets("Word_Specifications")
    .Select
    For Each cell In .Range(.Range("Word_Standard_Start").Address, .Range("Word_Standard_Start").End(xlDown).Address)
        If cell.value = "" Then
        Else
            L = Len(cell.value) + 1
            With Sh.Range(rng.Address)
                With .Characters(start:=cur_L, Length:=L).Font
                    .Name = "Arial"
                    .FontStyle = "Regular"
                    .Size = 9
                    .Bold = False
                    .Strikethrough = False
                    .Superscript = False
                    .Subscript = False
                    .OutlineFont = False
                    .Shadow = False
                    .Underline = xlUnderlineStyleNone
                    .ThemeColor = xlThemeColorLight1
                    .TintAndShade = 0
                    .ThemeFont = xlThemeFontNone
                End With

                cur_L = cur_L + L

                If .value <> "" Then
                    add_Wingdings cur_L, 1, Sh, rng
                    cur_L = cur_L + 2
                End If
            End With
        End If
    Next
End With


End Sub

Sub add_Wingdings(start As Integer, Length As Integer, Sh As Worksheet, rng As Range)
With Sh.Range(rng.Address).Characters(start:=start, Length:=Length).Font
    .Name = "Wingdings 3"
    .FontStyle = "Regular"
    .Size = 9
    .Bold = False
    .Strikethrough = False
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .Underline = xlUnderlineStyleNone
    .ThemeColor = xlThemeColorLight1
    .TintAndShade = 0
    .ThemeFont = xlThemeFontNone
End With
End Sub

Upvotes: 0

Related Questions