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
 

Some videos you may like

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Darren Bartrup

Well-known Member
Joined
Mar 13, 2006
Messages
1,291
Office Version
365
Platform
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,291
Office Version
365
Platform
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.
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,805
Messages
5,513,512
Members
408,955
Latest member
BodanKaraoke

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...
Top