limit to rangetohtml(rng)?

daveyc18

Well-known Member
Joined
Feb 11, 2013
Messages
790
Office Version
  1. 365
  2. 2010
Code:
   .htmlbody = strbody1 & " " & RangetoHTML(rng) & "<br><br>" & RangetoHTML(rng2) & "<br><br>" & RangetoHTML(rng3) & "<br><br>" & RangetoHTML(rng4) & "<br><br>" & RangetoHTML(rng5) & vbNewLine & Signature
     

   Set rng = Sheets(" Summary").Range("email_summary")
       
       Set rng2 = Sheets("FIN.PROD").Range("email_summary_2")
      
             Set rng3 = Sheets("Average Price").Range("email_summary_3")
      
              Set rng4 = Sheets("Top 10").Range("email_summary_4")
      
            Set rng5 = Sheets("Concentration Test").Range("email_summary_5")



seems like I can only do up to two..
 
Works for me:
1738997086765.png

Try:
VBA Code:
Sub Filter_and_Mail()
  Dim rng1 As Range, rng2 As Range, rng3 As Range, rng4 As Range, rng5 As Range
  Dim strbody1 As String, Signature As String
  
  Set rng1 = Sheets("Summary").Range("email_summary")
  Set rng2 = Sheets("FIN.PROD").Range("email_summary_2")
  Set rng3 = Sheets("Average Price").Range("email_summary_3")
  Set rng4 = Sheets("Top 10").Range("email_summary_4")
  Set rng5 = Sheets("Concentration Test").Range("email_summary_5")
  
  strbody1 = "Hello"
  Signature = "Bye"
  With CreateObject("Outlook.Application").CreateItem(0)
    .To = "danteamor@correo.com"
    .Subject = "range html"
    .HTMLbody = strbody1 & " " & RangetoHTML(rng1) & "<br>" & RangetoHTML(rng2) & "<br>" & _
                                 RangetoHTML(rng3) & "<br>" & RangetoHTML(rng4) & "<br>" & _
                                 RangetoHTML(rng5) & vbNewLine & Signature
    .Display
  End With
End Sub

Function RangetoHTML(rng As Range)
  ' Changed by Ron de Bruin 28-Oct-2006
  ' Working in Office 2000-2016
  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
 

Attachments

  • 1738996575700.png
    1738996575700.png
    11.4 KB · Views: 0
Upvote 0

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