Insert paragraph breaks in VBA

mosh

Board Regular
Joined
Jun 18, 2006
Messages
54
Hey All,

I have a piece of code that selects a range in my workbook and then email this out. For some reason, I can't seem to insert paragraph breaks. I have attached the code in question:

With OutMail
.to = sTo
.CC = ""
.BCC = ""
.Subject = "KPIs" & Worksheets("Summary_Page").Range("D18")
.HTMLBody = "All, " & "Please find the latest KPIs below: " & RangetoHTML(rng) & "Thank You" .Display
Application.Wait (Now + TimeValue("0:00:02"))
Application.SendKeys "%s"

I would like to add a new paragraph line after "All" and also after "Please find latest..."

Your help is greatly appreciated.

Thanks,
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Perhaps.
HTML:
.HTMLBody = "All, <p>Please find the latest KPIs below:<p> " & RangetoHTML(rng) & "Thank You, Strategy And BI" .Display
 
Upvote 0
Perhaps.
HTML:
.HTMLBody = "All, <p>Please find the latest KPIs below:<p> " & RangetoHTML(rng) & "Thank You, Strategy And BI"
 
Upvote 0
Thank you so much! That's perfect.

Also, Is there any way to keep the format of the excel cells in the html part of the code?

Thanks,
 
Upvote 0
I thought RangeToHTML would do that as, if I recall correctly, it saves the range, formatting and all, as HTML.

Are you losing some of the formatting?
 
Upvote 0
This is what I use at the moment to grab the cells and convert into HTML:

Function RangetoHTML(rng As Range)
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook

I've got a feeling it's something to do with the paste special?

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's a well known function, and as I said it saves/publishes the range passed to it.

I thought that retained the formatting, and the few times I've used it that's been the case.

Is it some particular formatting you are losing?
 
Upvote 0
It's a well known function, and as I said it saves/publishes the range passed to it.

I thought that retained the formatting, and the few times I've used it that's been the case.

Is it some particular formatting you are losing?

It's literally just a paste values and a basic table. I'm using excel 2007 and wanted the formatting preserved.
 
Upvote 0
That code should retain the formatting.

If it doesn't, what formatting are you losing?
 
Upvote 0
I lose the colour of the table and font. It's as if only the values are taken and a basic table.
 
Upvote 0

Forum statistics

Threads
1,223,277
Messages
6,171,150
Members
452,383
Latest member
woodsfordg

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