Advertisement
dave3009

ThaerMohamd

Mar 17th, 2020
757
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2.  
  3. Sub LoopSheetsSendMail()
  4.  
  5. ' Arrays to hold sheet and range info
  6. Dim shArray As Variant
  7. Dim rngArray As Variant
  8. Dim i As Integer ' because we need an iterator
  9. ' Some variables for email info
  10. Dim toLst As String
  11. Dim subj As String
  12. Dim rng As String
  13.  
  14. shArray = Array("FirstSheet", "SecondSheet", "ThirdSheet") '<--Change these to the tab names
  15. rngArray = Array("A1:B2", "A3:B7", "A1:C6") '<--Change these to the ranges you need, in order of the sheets above.
  16.  
  17. For i = LBound(shArray) To UBound(shArray)
  18.     With ThisWorkbook.Sheets(shArray(i))
  19.         toLst = .Range("F1")
  20.         ' repeat the above for cc and bcc is necessary
  21.        rng = rngArray(i)
  22.         subj = "Currently sending sheet - " & shArray(i)
  23.          ' send the mail
  24.        Mail_Selection_Range_Outlook_Body toLst, subj, .Name, rng
  25.     End With
  26. Next i
  27. End Sub
  28.  
  29. ' All the code below was taken from - https://www.rondebruin.nl/win/s1/outlook/bmail2.htm
  30. ' some minor adapations made
  31. Sub Mail_Selection_Range_Outlook_Body(toLst As String, subj As String, sh As String, bdyRng As String)
  32. 'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
  33. 'Don't forget to copy the function RangetoHTML in the module.
  34. 'Working in Excel 2000-2016
  35.    Dim rng As Range
  36.     Dim OutApp As Object
  37.     Dim OutMail As Object
  38.  
  39.     Set rng = Nothing
  40.     On Error Resume Next
  41.     'Only the visible cells in the selection
  42.    Set rng = ThisWorkbook.Sheets(sh).Range(bdyRng)
  43.     'You can also use a fixed range if you want
  44.    'Set rng = Sheets("YourSheet").Range("D4:D12").SpecialCells(xlCellTypeVisible)
  45.    On Error GoTo 0
  46.  
  47.     If rng Is Nothing Then
  48.         MsgBox "The selection is not a range or the sheet is protected" & _
  49.                vbNewLine & "please correct and try again.", vbOKOnly
  50.         Exit Sub
  51.     End If
  52.  
  53.     With Application
  54.         .EnableEvents = False
  55.         .ScreenUpdating = False
  56.     End With
  57.  
  58.     Set OutApp = CreateObject("Outlook.Application")
  59.     Set OutMail = OutApp.CreateItem(0)
  60.  
  61.     On Error Resume Next
  62.     With OutMail
  63.         .To = toLst
  64.         .CC = ""
  65.         .BCC = ""
  66.         .Subject = subj
  67.         .HTMLBody = RangetoHTML(rng)
  68.         .Send   'or use .Display
  69.    End With
  70.     On Error GoTo 0
  71.  
  72.     With Application
  73.         .EnableEvents = True
  74.         .ScreenUpdating = True
  75.     End With
  76.  
  77.     Set OutMail = Nothing
  78.     Set OutApp = Nothing
  79. End Sub
  80.  
  81.  
  82. Function RangetoHTML(rng As Range)
  83. ' Changed by Ron de Bruin 28-Oct-2006
  84. ' Working in Office 2000-2016
  85.    Dim fso As Object
  86.     Dim ts As Object
  87.     Dim TempFile As String
  88.     Dim TempWB As Workbook
  89.  
  90.     TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
  91.  
  92.     'Copy the range and create a new workbook to past the data in
  93.    rng.Copy
  94.     Set TempWB = Workbooks.Add(1)
  95.     With TempWB.Sheets(1)
  96.         .Cells(1).PasteSpecial Paste:=8
  97.         .Cells(1).PasteSpecial xlPasteValues, , False, False
  98.         .Cells(1).PasteSpecial xlPasteFormats, , False, False
  99.         .Cells(1).Select
  100.         Application.CutCopyMode = False
  101.         On Error Resume Next
  102.         .DrawingObjects.Visible = True
  103.         .DrawingObjects.Delete
  104.         On Error GoTo 0
  105.     End With
  106.  
  107.     'Publish the sheet to a htm file
  108.    With TempWB.PublishObjects.Add( _
  109.          SourceType:=xlSourceRange, _
  110.          Filename:=TempFile, _
  111.          Sheet:=TempWB.Sheets(1).Name, _
  112.          Source:=TempWB.Sheets(1).UsedRange.Address, _
  113.          HtmlType:=xlHtmlStatic)
  114.         .Publish (True)
  115.     End With
  116.  
  117.     'Read all data from the htm file into RangetoHTML
  118.    Set fso = CreateObject("Scripting.FileSystemObject")
  119.     Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
  120.     RangetoHTML = ts.readall
  121.     ts.Close
  122.     RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
  123.                           "align=left x:publishsource=")
  124.  
  125.     'Close TempWB
  126.    TempWB.Close savechanges:=False
  127.  
  128.     'Delete the htm file we used in this function
  129.    Kill TempFile
  130.  
  131.     Set ts = Nothing
  132.     Set fso = Nothing
  133.     Set TempWB = Nothing
  134. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement