Advertisement
Guest User

Untitled

a guest
Oct 20th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. Sub OutlookToExcel()
  2.  
  3. Dim appExcel As Excel.Application
  4. Dim wkb As Excel.Workbook
  5. Dim wks As Excel.Worksheet
  6. Dim rng As Excel.Range
  7. Dim strPath As String
  8. Dim intRowCounter As Integer
  9. Dim intColumnCounter As Integer
  10. Dim Msg As Outlook.MailItem
  11. Dim nms As Outlook.NameSpace
  12. Dim fld As Outlook.MAPIFolder
  13. Dim itm As Object
  14.  
  15. strPath = "C:UsersmeDocumentsAction ItemsWMV 856 load.xlsm"
  16. Debug.Print strSheet
  17. Set nms = Application.GetNamespace("MAPI")
  18. Set fld = nms.GetDefaultFolder(olFolderInbox).Folders("WMV Test")
  19.  
  20. 'Open and activate Excel workbook.
  21. Set appExcel = CreateObject("Excel.Application")
  22. appExcel.Workbooks.Open (strPath)
  23. Set wkb = appExcel.ActiveWorkbook
  24. Set wks = wkb.Sheets(2)
  25. wks.Activate
  26. appExcel.Application.Visible = True
  27.  
  28. 'Copy field items in mail folder.
  29. For Each itm In fld.Items
  30. intColumnCounter = 1
  31. Set Msg = itm
  32. intRowCounter = intRowCounter + 1
  33. Set rng = wks.Cells(intRowCounter, intColumnCounter)
  34. rng.Value = Msg.Body
  35. intColumnCounter = intColumnCounter + 1
  36. Next itm
  37.  
  38. 'Move items
  39. ' Set Vars
  40. Dim SubFolder As Outlook.MAPIFolder
  41. Dim Item As Object
  42. Dim lngCount As Long
  43. Dim Items As Outlook.Items
  44.  
  45. ' Set Items Reference
  46. Set Items = fld.Items
  47.  
  48. ' Loop through the Items
  49. For lngCount = Items.Count To 1 Step -1
  50. Set Item = Items.Item(lngCount)
  51.  
  52. Debug.Print Item.Subject
  53.  
  54. If Item.Class = olMail Then
  55. ' // Set SubFolder of Inbox
  56. Set SubFolder = nms.GetDefaultFolder(olFolderInbox).Folders("WMV Done")
  57. ' // Mark As Read
  58. Item.UnRead = False
  59. ' // Move Mail Item to sub Folder
  60. Item.Move SubFolder
  61. End If
  62. Next lngCount
  63.  
  64.  
  65.  
  66. SplitTextColumn
  67.  
  68. MakeOneColumn
  69.  
  70. Set appExcel = Nothing
  71. Set wkb = Nothing
  72. Set wks = Nothing
  73. Set rng = Nothing
  74. Set Msg = Nothing
  75. Set nms = Nothing
  76. Set fld = Nothing
  77. Set itm = Nothing
  78. Exit Sub
  79. Set appExcel = Nothing
  80. Set wkb = Nothing
  81. Set wks = Nothing
  82. Set rng = Nothing
  83. Set Msg = Nothing
  84. Set nms = Nothing
  85. Set fld = Nothing
  86. Set itm = Nothing
  87. End Sub
  88.  
  89. Sub SplitTextColumn()
  90. 'Takes all data out of one cell and splits it by line
  91.  
  92. Dim i As Long
  93. Dim vA As Variant
  94.  
  95.  
  96. [A1].Select '<~~~ sometimes on this line
  97. Range(Selection, Selection.End(xlDown)).Select
  98. For i = 1 To Selection.Rows.Count
  99. vA = Split(Selection.Resize(1).Offset(i - 1), vbLf)
  100. Selection.Offset(i - 1).Resize(1, UBound(vA) + 1).Offset(, 1) = vA '<~~~ sometimes on this line
  101. Next
  102.  
  103. [A1].CurrentRegion.Offset(0, 1).Select
  104. Selection.Copy
  105. Sheets.Add After:=ActiveSheet
  106. Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
  107. False, Transpose:=True
  108.  
  109.  
  110. End Sub
  111.  
  112. Sub MakeOneColumn()
  113. 'Takes data and stacks it in a single column (A)
  114.  
  115. Dim vaCells As Variant
  116. Dim vOutput() As Variant
  117. Dim i As Long, j As Long
  118. Dim lRow As Long
  119.  
  120. If TypeName(Selection) = "Range" Then
  121. If Selection.Count > 1 Then
  122. If Selection.Count <= Selection.Parent.Rows.Count Then
  123. vaCells = Selection.Value
  124.  
  125. ReDim vOutput(1 To UBound(vaCells, 1) * UBound(vaCells, 2), 1 To 1)
  126.  
  127. For j = LBound(vaCells, 2) To UBound(vaCells, 2)
  128. For i = LBound(vaCells, 1) To UBound(vaCells, 1)
  129. If Len(vaCells(i, j)) > 0 Then
  130. lRow = lRow + 1
  131. vOutput(lRow, 1) = vaCells(i, j)
  132. End If
  133. Next i
  134. Next j
  135.  
  136. Selection.ClearContents
  137. Selection.Cells(1).Resize(lRow).Value = vOutput
  138. End If
  139. End If
  140. End If
  141.  
  142. Dim c As Range
  143. Set rng = ActiveSheet.Range("A1:A5000")
  144. For dblCounter = rng.Cells.Count To 1 Step -1
  145. Set c = rng(dblCounter)
  146. If c.Value Like "*MEADWESTVACO SUMMARY 856*" Then
  147. c.EntireRow.Insert
  148. End If
  149. Next dblCounter
  150.  
  151. Sheets("Data").[A1].CurrentRegion.Delete
  152.  
  153. Sheets("Sheet2").Range("A1", Cells(Rows.Count, 1).End(xlUp)).Copy Destination:=Sheets("Data").Range("A1")
  154.  
  155. Sheets("Data").Range("A1").EntireRow.Delete
  156.  
  157. Sheets("Data").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).FormulaR1C1 = "LAST LINE"
  158.  
  159. Sheets("Sheet2").Delete
  160.  
  161. End Sub
  162.  
  163.  
  164. Sub MoveToDataSheet()
  165.  
  166.  
  167. Sheets("Data").[A1].CurrentRegion.Delete
  168.  
  169. Sheets("Sheet2").[A:A].Copy Destination:=Sheets("Data").Range("A1")
  170.  
  171. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement