Advertisement
tooms03

Outlook project

Apr 24th, 2017
818
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Private WithEvents myOlItems  As Outlook.Items  'These are all the variables accessible from all the subs
  2. Private sujet As String
  3. Private bonjour As String
  4. Private Body As String
  5. Private L As Integer
  6. Private t As Integer
  7.  
  8. Private Sub Application_Startup()  'Macro that will set the variables in order to use the current outlook address
  9.    Dim olApp As Outlook.Application
  10.     Dim objNS As Outlook.NameSpace
  11.       Set olApp = Outlook.Application
  12.       Set objNS = olApp.GetNamespace("MAPI")
  13.       Set myOlItems = objNS.GetDefaultFolder(olFolderInbox).Items
  14. End Sub
  15.  
  16. Private Sub myOlItems_ItemAdd(ByVal item As Object)  'Macro that will check when we get a new email
  17.  
  18. Stop
  19.  
  20. On Error GoTo ErrorHandler  'If an error occurs, go to the label ErrorHandler
  21.  
  22.   Dim Msg As Outlook.MailItem
  23.   Dim myDestFolder As Outlook.Folder
  24.   Dim myInbox As Outlook.Folder
  25.   Dim myNameSpace As Outlook.NameSpace
  26.  
  27.   'MsgBox "New Email" 'To check if the macro is well running when we get a new email
  28.  
  29.   If TypeName(item) = "MailItem" Then
  30.     Set myNameSpace = Application.GetNamespace("MAPI")  'Use the current session in outlook
  31.    Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)  'Use the current inbox
  32.    Set Msg = item
  33.     Set myDestFolder = myInbox.Folders("Cycles Count")  'Set the destination folder to a Folder named Cycles Count in the Inbox folder
  34.    
  35.     sujet = Msg.Subject  'Save the subject of the new mail
  36.    Body = Msg.Body      'Save the boody of the new mail
  37.    
  38.     t = 0                'set the variable t to 0, necessary to be reseted before every loop
  39.    LoopThroughFiles (sujet) 'Call the macro LoopThroughFiles using the parameter suject which is the subject of the new mail
  40.    
  41.     If t = 1 Then  'If the file exist in the folder
  42.    
  43.     Add_Sheet_ClosedBook 'Call the macro Add_Sheet_CloseBook
  44.    
  45.     Msg.Move myDestFolder 'Call the macro myDestFolder
  46.  
  47.     End If
  48.  
  49.   End If
  50.  
  51. ProgramExit: 'define the label ProgramExit
  52.  Exit Sub
  53. ErrorHandler: 'define the label ErrorHandler
  54.  MsgBox Err.Number & " - " & Err.Description 'Display the error in a message box
  55.  Resume ProgramExit
  56. End Sub
  57.  
  58. Private Sub Add_Sheet_ClosedBook() 'Macro that will modify the workbook
  59.    Dim bk As Workbook
  60.     Dim sh As Worksheet
  61.     Dim shName As String
  62.     Dim arr(1 To 12) As Variant
  63.    
  64.     Set bk = Workbooks.Open _
  65.     ("H:\" & sujet & ".xlsm")  'Open the workbook named sujet.xlsm in this folder
  66.                                                            'This path between the " " could be change
  67.    
  68.     L = bk.Worksheets.Count  'Count the number of sheets in the workbook
  69.    
  70.     Dim line As String
  71.    
  72.     For i = 1 To L - 2  'Loop through each line
  73.    
  74.     line = bk.Sheets(i + 1).name 'Save the name of the first line. The +1 is needed to exclude the main sheet from the loop
  75.    cycle = ParseTextLinePair(Body, line & ": ") 'Call the function ParseTextLinePair for the body and the parameter "line: "
  76.    
  77.    
  78.     Sheets(i + 1).Activate 'Activate the main sheet
  79.    
  80.     For j = 9 To 300 'Loop through the 300 first lines
  81.    
  82.     If Cells(j, 6) = "" Then  'Check the first empty cell in the column
  83.    
  84.     li = j - 9
  85.     GoTo Label7  'Go out of the loop to the label suite
  86.    End If
  87.    
  88.     Next
  89.    
  90. Label7:
  91.  
  92. Row = j - 1
  93.    
  94.      If Row < 9 Then GoTo Label3
  95.    
  96.     If Cells(Row, 9) = "ok" And Cells(Row, 8) = "" And Cells(9 + ok + 1, 2) <> "" Then
  97.     Range("L15") = Cells(Row, 4)
  98.     Range("L16") = Range("L16") + 1
  99.     Cells(Row, 9) = ""
  100.     Cells(Row, 7) = "Updated"
  101.     End If
  102.    
  103.     If Cells(Row, 9) = "ok" And Cells(Row, 8) = "" Then
  104.     Range("L15") = Cells(Row, 4)
  105.     Cells(Row, 9) = ""
  106.     Cells(Row, 7) = "Updated"
  107.     End If
  108.    
  109.     If Cells(Row, 9) = "ok" And Cells(Row, 8) <> "" Then
  110.     Range("L15") = Cells(Row, 4)
  111.     Range("L16") = Range("L16") + 1
  112.     Cells(Row, 9) = ""
  113.     Cells(Row, 7) = "Updated"
  114.     End If
  115.    
  116.     ok = Range("L16")
  117.    
  118.     Sheets(1).Activate
  119.    
  120.     For j = 5 To 300 'Loop through the 300 first lines
  121.    
  122.     If IsEmpty(Cells(j, 4 + 2 * (i - 1))) = True Then  'Check the first empty cell in the column
  123.    
  124.     Cells(j, 4 + 2 * (i - 1)) = cycle 'Copy the cycle value in this cell
  125.    
  126.     GoTo suite 'Go out of the loop to the label suite
  127.    
  128.     End If
  129.    
  130.     Next
  131.    
  132. suite:
  133.  
  134.     Sheets(i + 1).Activate
  135.    
  136. If IsEmpty(Cells(Row, 8)) = True Then
  137.    
  138.     Next_date1 = Cells(9 + ok, 2) + Range("L15") * 7 * (li - ok)
  139.     Next_date1 = Mid(Next_date1, 1, 10)
  140.    
  141.     Next_date2 = Cells(Row, 2) + Cells(Row, 4) * 7
  142.     Next_date2 = Mid(Next_date2, 1, 10)
  143.    
  144.     Else
  145.    
  146.     Next_date1 = Cells(9 + ok, 2) + Range("L15") * 7 * (li + 1 - ok)
  147.     Next_date1 = Mid(Next_date1, 1, 10)
  148.    
  149.     Next_date2 = Cells(Row, 8) + Cells(Row, 4) * 7
  150.     Next_date2 = Mid(Next_date2, 1, 10)
  151.    
  152.     End If
  153.    
  154.     Diff = DateDiff("d", Next_date2, Next_date1)
  155.    
  156.     Diff = Abs(Diff)
  157.    
  158.     If Diff >= 21 Then
  159.     'MsgBox "Too big gap with the line" & i - 1 & ""
  160.    arr(i) = line
  161.     issue = 1
  162.     End If
  163.    
  164. Label3:
  165.  
  166.  
  167. Next
  168.  
  169.     If issue = 1 Then
  170.     sendOutlookEmail arr
  171.     End If
  172.  
  173. '=========================
  174.    
  175.     ActiveWorkbook.Close SaveChanges:=True 'Close the workbook and save the modifications
  176.    
  177. End Sub
  178.  
  179. Function ParseTextLinePair(strSource As String, strLabel As String) 'Macro that will check into a text for some specific characters and return what is just after it
  180.    Dim intLocLabel As Integer
  181.     Dim intLocCRLF As Integer
  182.     Dim intLenLabel As Integer
  183.     Dim strText As String
  184.      
  185.     ' locate the label in the source text
  186.    intLocLabel = InStr(strSource, strLabel)
  187.     intLenLabel = Len(strLabel)
  188.         If intLocLabel > 0 Then
  189.         intLocCRLF = InStr(intLocLabel, strSource, vbCrLf)
  190.         If intLocCRLF > 0 Then
  191.             intLocLabel = intLocLabel + intLenLabel
  192.             strText = Mid(strSource, _
  193.                             intLocLabel, _
  194.                             intLocCRLF - intLocLabel)
  195.         Else
  196.             intLocLabel = Mid(strSource, intLocLabel + intLenLabel)
  197.         End If
  198.     End If
  199.     ParseTextLinePair = Trim(strText)
  200. End Function
  201.  
  202. Sub LoopThroughFiles(nam)    'Macro that will check if there is a file in the folder with the name given by the parameter nam
  203.    Dim MyObj As Object, MySource As Object, file As Variant
  204.    file = Dir("C:\Users\pirenne\Desktop\Cycles\")  'Define the path of the folder, this one could be changed
  205.   While (file <> "") 'While the file name is different from "", which means that there is a file
  206.      If InStr(file, nam) > 0 Then 'If the file name contain the nam value
  207.         'MsgBox "found "           'Message box to inform the user, could be deleted eventually
  208.         t = 1                     'Set the variable t to 1 in order to not look after a workbook that doesn't exist in the folder
  209.         GoTo fin                  'Go to label fin because there is only one file with this name in the folder
  210.      End If
  211.      file = Dir                    'For the next file
  212.  Wend
  213. fin:                               'Define the label fin
  214. End Sub
  215.  
  216. Sub sendOutlookEmail(ligne() As Variant)
  217. Dim oApp As Outlook.Application
  218. Dim oMail As MailItem
  219. Set oApp = CreateObject("Outlook.application")
  220.  
  221. text = "The services planned for the above lines have changed of more than 3 weeks. Please update the planning for these lines."
  222.  
  223. For i = 1 To 12
  224. If ligne(i) <> 0 Then
  225. text = text + "" & vbNewLine & " - " & ligne(i) & ""
  226. End If
  227. Next
  228.  
  229. Set oMail = oApp.CreateItem(olMailItem)
  230. oMail.Body = text
  231. oMail.Subject = "Services Plannification for " & sujet & ""
  232. oMail.To = "thomas.pirenne@hotmail.com"
  233. oMail.Send
  234. Set oMail = Nothing
  235. Set oApp = Nothing
  236.  
  237.  
  238. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement