Ron de Bruin RangetoHTML

faliero

New Member
Joined
May 13, 2005
Messages
42
The wonderful RangetoHTML function by Ron de Bruin doesn't work any more.
http://www.rondebruin.nl/mail/folder3/mail4.htm
I'm using Excel 2007.
It doesn't copy the range on the mail message and doesn't close the temporary excel file.

I'm a desperate man!!! Any help or suggestion is very welcome and appreciated
Code:
Option Explicit

Sub Mail_Selection_Range_Outlook_Body()


    Dim rng As Range
    Dim OutApp As Outlook.Application
    Dim OutMail As Outlook.MailItem

    Set rng = Nothing
    On Error Resume Next
        Set rng = ActiveSheet.Range("A1:D20").SpecialCells(xlCellTypeVisible)
    On Error GoTo 0

    If rng Is Nothing Then
        MsgBox "The selection is not a range or the sheet is protected" & _
               vbNewLine & "please correct and try again.", vbOKOnly
        Exit Sub
    End If

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(olMailItem)
    
    On Error Resume Next
    With OutMail
        .To = ""
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .HTMLBody = RangetoHTML(rng)
        .Display
    End With
    On Error GoTo 0

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub

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 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
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

Darren Bartrup

Well-known Member
Joined
Mar 13, 2006
Messages
1,296
Office Version
  1. 365
Platform
  1. Windows
Works fine for me, though the code you posted will always use
Code:
ActiveSheet.Range("A1:D20").SpecialCells(xlCellTypeVisible)

Have you set references to Outlook as it's using early binding?
 

Darren Bartrup

Well-known Member
Joined
Mar 13, 2006
Messages
1,296
Office Version
  1. 365
Platform
  1. Windows
It depends on what part of the spreadsheet you want posting to the email - if it's always the range A1:D20 on the active sheet then it's fine.

I'd use:
Code:
Set rng = Selection.SpecialCells(xlCellTypeVisible)

so it will transfer the currently selected range.

If you've set the reference properly then it could be something to do with 2007 - sorry, missed that part in your original post as I'm using 2003 and it's working fine on this.

Just tried it on 2007 and it worked to - must be a change to the Outlook 12.0 library as I'm having to use 11.0.

Try stepping through the code a line at a time using F5 to see where it falls over.
 

faliero

New Member
Joined
May 13, 2005
Messages
42
It depends on what part of the spreadsheet you want posting to the email - if it's always the range A1:D20 on the active sheet then it's fine.

I'd use:
Code:
Set rng = Selection.SpecialCells(xlCellTypeVisible)

so it will transfer the currently selected range.

If you've set the reference properly then it could be something to do with 2007 - sorry, missed that part in your original post as I'm using 2003 and it's working fine on this.

Just tried it on 2007 and it worked to - must be a change to the Outlook 12.0 library as I'm having to use 11.0.

Try stepping through the code a line at a time using F5 to see where it falls over.

Thanks for the help.
I've been using the function on several sheets for more than a year and suddenly the function stopped working about 1 month ago.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,168,109
Messages
5,857,439
Members
431,879
Latest member
KiwDaWabbit

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
Top