Email through Excel, text formatting (VBA)

NewUser2

Board Regular
Joined
Jan 27, 2015
Messages
54
Okay so I am having a problem. If you enter this code (below) in a VBA module you will have the desired text show up in the email body and to the desired recipients (yay). HOWEVER, I need to find a way to have my formatted text appear as it is in excel. ie bolded and underlined at certain points.

Please, any suggestions would be greatly appreciated. (if you can get it to work, maybe just paste in the new code?)

Sub CreateMail()


Dim doData1 As DataObject, doData2 As DataObject
Dim objOutlook As Object, objMail As Object
Dim rngTo As Range, rngCc As Range, rngSubject As Range
Dim rngBody1 As Range, rngBody2 As Range

Set doData1 = New DataObject
Set doData2 = New DataObject
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)


With Sheets("Sheet1")
Set rngTo = .Range("D2")
Set rngCc = .Range("D3")
Set rngSubject = .Range("H1")
End With

'This is the selection for the body of the email.
Set rngBody1 = Sheets("Sheet1").Range("A1:C10")
rngBody1.Copy
doData1.GetFromClipboard


'I unlock these if I want to add data from another sheet or a new range from the same sheet
'Set rngBody2 = Sheets("Sheet3").Range("B1")
'rngBody2.Copy
'doData2.GetFromClipboard

With objMail
.To = rngTo.Value
.CC = rngCc.Value
.Subject = rngSubject.Value
.Body = doData1.GetText(1) '(this section is locked and unlocked if I want to use a second range) & vbCrLf & doData2.GetText(1)
.Send
End With

Set doData1 = Nothing
Set doData2 = Nothing
Set objOutlook = Nothing
Set objMail = Nothing
Set rngTo = Nothing
Set rngCc = Nothing
Set rngSubject = Nothing
Set rngBody1 = Nothing
Set rngBody2 = Nothing


End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
I did search around, and setting it to <b> with .HTMLBody doesn't work for mine... As you see in my code, to get the desired cells in the body I am selecting a range which collects a table (see below), for the table I only want to headings to be bold, not all the text in the table.

I would be fine copying and pasting a picture if someone knows how to go about that... As long as my range can appear as it is in

CurrentChange
US
3M0.020.01
1Y0.240.00
2Y0.69-0.01
5Y1.62-0.01
10y2.13-0.01
30Y2.71-0.01
Canada
3M0.580.00
1Y0.630.00
2Y0.600.01
5Y0.92-0.01
10y1.52-0.01
30Y2.170.00


<colgroup><col span="2"><col></colgroup><tbody>
</tbody>
 
Upvote 0
Hi,

If you use RangeToHTML with HTMLBody it will copy the range to Outlook with the Excel formatting.

I used the following to test using your code
Code:
.HTMLBody = RangetoHTML(rngBody1)
Have a play it should suit.

Code:
Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2013
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook
    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
    'Copy the range and create a new workbook to past the data in
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With
    'Publish the sheet to a htm file
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         FileName:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With
    'Read all data from the htm file into RangetoHTML
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.readall
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")
    'Close TempWB
    TempWB.Close savechanges:=False
    'Delete the htm file we used in this function
    Kill TempFile
    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function
 
Upvote 0
It Worked!! Thank you!

Hi,

If you use RangeToHTML with HTMLBody it will copy the range to Outlook with the Excel formatting.

I used the following to test using your code
Code:
.HTMLBody = RangetoHTML(rngBody1)
Have a play it should suit.

Code:
Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2013
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook
    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
    'Copy the range and create a new workbook to past the data in
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With
    'Publish the sheet to a htm file
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         FileName:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With
    'Read all data from the htm file into RangetoHTML
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.readall
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")
    'Close TempWB
    TempWB.Close savechanges:=False
    'Delete the htm file we used in this function
    Kill TempFile
    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function
 
Upvote 0

Forum statistics

Threads
1,214,861
Messages
6,121,969
Members
449,059
Latest member
oculus

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