Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2010
Dim FSO As Object
Dim ts As Object
Dim RowDifference As String
Dim TempFile As String
Dim HomeWB As Workbook
Dim TempWB As Workbook
Dim lngLastRow As Long
Dim rngConsider As Range
Dim lngRows As Long
Dim lngVisibleRows As Long
Dim Offset As Integer
Dim lngHiddenRows As Long
Dim i As Long
Dim RowTotal As Long
Dim HyperL As Hyperlink
TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
'Copy the range and create a new workbook to paste the data in
rng.Copy
'Debug.Print rng.Address
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
'Calculate how far from the top the row is (this is used to offset hyperlinks because we paste into the first cell of the temporary workbook which shifts everything from the original sheet up when pasted into the temp sheet
RowDifference = (rng.Cells(1, 1).Row - Cells(1, 1).Row) * -1
'~~~~~~~~~~~~~~~~~~~~~~~~~~CONVERT AND SHIFT HYPERLINKS~~~~~~~~~~~~~~~~~~~~~~~
For Each HyperL In rng.Hyperlinks
TempWB.Sheets(1).Hyperlinks.Add _
Anchor:=TempWB.Sheets(1).Range(HyperL.Range.Address).Offset(RowDifference), _
Address:=HyperL.Address, _
TextToDisplay:=HyperL.TextToDisplay
Next HyperL
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'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