Guest User

Untitled

a guest
Jul 2nd, 2018
1,070
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.52 KB | None | 0 0
  1. +----------------+---------------------+---------+
  2. | Email | Application | Version |
  3. +----------------+---------------------+---------+
  4. | test1@test.com | Microsoft_Office_13 | v2.0 |
  5. | test1@test.com | Putty | v3.0 |
  6. | test1@test.com | Notepad | v5.6 |
  7. | test2@test.com | Microsoft_Office_13 | v2.0 |
  8. | test2@test.com | Putty | v3.0 |
  9. | test2@test.com | Adobe_Reader | v6.4 |
  10. | test3@test.com | Microsoft_Office_13 | v3.6 |
  11. | test3@test.com | Paint | v6.4 |
  12. | test3@test.com | Adobe_Reader | v6.4 |
  13. +----------------+---------------------+---------+
  14.  
  15. +----------------+---------------------+---------+
  16. | Email | Application | Version |
  17. +----------------+---------------------+---------+
  18. | test2@test.com | Microsoft_Office_13 | v2.0 |
  19. | test2@test.com | Putty | v3.0 |
  20. | test2@test.com | Adobe_Reader | v6.4 |
  21. +----------------+---------------------+---------+
  22.  
  23. +----------------+---------------------+---------+
  24. | Email | Application | Version |
  25. +----------------+---------------------+---------+
  26. | test2@test.com | Microsoft_Office_13 | v2.0 |
  27. | test2@test.com | Putty | v3.0 |
  28. | test2@test.com | Adobe_Reader | v6.4 |
  29. +----------------+---------------------+---------+
  30.  
  31. Option Explicit
  32.  
  33.  
  34. Sub Test1()
  35. Dim OutApp As Object
  36. Dim OutMail As Object
  37. Dim dict As Object 'keep the unique list of emails
  38. Dim cell As Range
  39. Dim cell2 As Range
  40. Dim rng As Range
  41. Dim i As Long
  42. Dim WS As Worksheet
  43.  
  44. Application.ScreenUpdating = False
  45. Set OutApp = CreateObject("Outlook.Application")
  46. Set dict = CreateObject("scripting.dictionary")
  47. Set WS = ThisWorkbook.Sheets("Sheet1") 'change the name of the sheet accordingly
  48.  
  49. On Error GoTo cleanup
  50. For Each cell In WS.Columns("A").Cells.SpecialCells(xlCellTypeConstants)
  51. If cell.Value Like "?*@?*.?*" Then
  52.  
  53. 'check if this email address has been used to generate an outlook email or not
  54. If dict.exists(cell.Value) = False Then
  55.  
  56. dict.Add cell.Value, "" 'add the new email address
  57. Set OutMail = OutApp.CreateItem(0)
  58. Set rng = WS.UsedRange.Rows(1)
  59.  
  60. 'find all of the rows with the same email and add it to the range
  61. For Each cell2 In WS.UsedRange.Columns(1).Cells
  62. If cell2.Value = cell.Value Then
  63. Set rng = Application.Union(rng, WS.UsedRange.Rows(cell2.Row))
  64. End If
  65. Next cell2
  66.  
  67. On Error Resume Next
  68. With OutMail
  69. .To = cell.Value
  70. .Subject = "Reminder"
  71. .HTMLBody = "Hi, please find your account permissions below:" & vbNewLine & vbNewLine & RangetoHTML(rng)
  72. .Display
  73. End With
  74.  
  75. On Error GoTo 0
  76. Set OutMail = Nothing
  77. End If
  78. End If
  79. Next cell
  80.  
  81. cleanup:
  82. Set OutApp = Nothing
  83. Application.ScreenUpdating = True
  84. End Sub
  85.  
  86. Function RangetoHTML(rng As Range)
  87. ' coded by Ron de Bruin 28-Oct-2006
  88. ' Working in Office 2000-2016
  89. Dim fso As Object
  90. Dim ts As Object
  91. Dim TempFile As String
  92. Dim TempWB As Workbook
  93.  
  94. TempFile = Environ$("temp") & "" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
  95.  
  96. 'Copy the range and create a new workbook to past the data in
  97. rng.Copy
  98. Set TempWB = Workbooks.Add(1)
  99. With TempWB.Sheets(1)
  100. .Cells(1).PasteSpecial Paste:=8
  101. .Cells(1).PasteSpecial xlPasteValues, , False, False
  102. .Cells(1).PasteSpecial xlPasteFormats, , False, False
  103. .Cells(1).Select
  104. Application.CutCopyMode = False
  105. On Error Resume Next
  106. .DrawingObjects.Visible = True
  107. .DrawingObjects.Delete
  108. On Error GoTo 0
  109. End With
  110.  
  111. 'Publish the sheet to a htm file
  112. With TempWB.PublishObjects.Add( _
  113. SourceType:=xlSourceRange, _
  114. Filename:=TempFile, _
  115. Sheet:=TempWB.Sheets(1).Name, _
  116. Source:=TempWB.Sheets(1).UsedRange.Address, _
  117. HtmlType:=xlHtmlStatic)
  118. .Publish (True)
  119. End With
  120.  
  121. 'Read all data from the htm file into RangetoHTML
  122. Set fso = CreateObject("Scripting.FileSystemObject")
  123. Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
  124. RangetoHTML = ts.readall
  125. ts.Close
  126. RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
  127. "align=left x:publishsource=")
  128.  
  129. 'Close TempWB
  130. TempWB.Close savechanges:=False
  131.  
  132. 'Delete the htm file we used in this function
  133. Kill TempFile
  134.  
  135. Set ts = Nothing
  136. Set fso = Nothing
  137. Set TempWB = Nothing
  138. End Function
  139.  
  140. Option Explicit
  141.  
  142. Public app As String
  143. Public version As String
  144.  
  145. Option Explicit
  146.  
  147. Sub Consolidate()
  148.  
  149. #If Early Then
  150. Dim emailInformation As New Scripting.Dictionary
  151. #Else
  152. Dim emailInformation As Object
  153. Set emailInformation = CreateObject("Scripting.Dictionary")
  154. #End If
  155.  
  156. GetEmailInformation emailInformation
  157. SendInfoEmail emailInformation
  158. End Sub
  159.  
  160.  
  161. Sub GetEmailInformation(emailInformation As Object)
  162.  
  163. Dim rg As Range
  164. Dim sngRow As Range
  165.  
  166. Dim emailAddress As String
  167. Dim myAppInfo As AppInfo
  168. Dim AppInfos As Collection
  169.  
  170. Set rg = Range("A1").CurrentRegion ' Assuming the list starts in A1 and DOES NOT contain empty row
  171. Set rg = rg.Offset(1).Resize(rg.Rows.Count - 1) ' Cut the headings
  172.  
  173. For Each sngRow In rg.Rows
  174.  
  175. emailAddress = sngRow.Cells(1, 1)
  176.  
  177. Set myAppInfo = New AppInfo
  178. With myAppInfo
  179. .app = sngRow.Cells(1, 2)
  180. .version = sngRow.Cells(1, 3)
  181. End With
  182.  
  183. If emailInformation.Exists(emailAddress) Then
  184. emailInformation.item(emailAddress).Add myAppInfo
  185. Else
  186. Set AppInfos = New Collection
  187. AppInfos.Add myAppInfo
  188. emailInformation.Add emailAddress, AppInfos
  189. End If
  190.  
  191. Next
  192.  
  193. End Sub
  194. Sub SendInfoEmail(emailInformation As Object)
  195.  
  196. Dim sBody As String
  197. Dim sBodyStart As String
  198. Dim sBodyInfo As String
  199. Dim sBodyEnd As String
  200. Dim emailAdress As Variant
  201. Dim colLines As Collection
  202. Dim line As Variant
  203.  
  204. sBodyStart = "Hi, please find your account permissions below:" & vbCrLf
  205.  
  206.  
  207. For Each emailAdress In emailInformation
  208. Set colLines = emailInformation(emailAdress)
  209. sBodyInfo = ""
  210. For Each line In colLines
  211. sBodyInfo = sBodyInfo & _
  212. "Application: " & line.app & vbTab & "Version:" & line.version & vbCrLf
  213. Next
  214. sBodyEnd = "Best Regards" & vbCrLf & _
  215. "Team"
  216.  
  217. sBody = sBodyStart & sBodyInfo & sBodyEnd
  218. SendEmail emailAdress, "Permissions", sBody
  219. Next
  220.  
  221.  
  222. End Sub
  223.  
  224. Sub SendEmail(ByVal sTo As String _
  225. , ByVal sSubject As String _
  226. , ByVal sBody As String _
  227. , Optional ByRef coll As Collection)
  228.  
  229.  
  230. #If Early Then
  231. Dim ol As Outlook.Application
  232. Dim outMail As Outlook.MailItem
  233. Set ol = New Outlook.Application
  234. #Else
  235. Dim ol As Object
  236. Dim outMail As Object
  237. Set ol = CreateObject("Outlook.Application")
  238. #End If
  239.  
  240. Set outMail = ol.CreateItem(0)
  241.  
  242. With outMail
  243. .To = sTo
  244. .Subject = sSubject
  245. .Body = sBody
  246. If Not (coll Is Nothing) Then
  247. Dim item As Variant
  248. For Each item In coll
  249. .Attachments.Add item
  250. Next
  251. End If
  252.  
  253. .Display
  254. '.Send
  255. End With
  256.  
  257. Set outMail = Nothing
  258.  
  259. End Sub
Add Comment
Please, Sign In to add comment