Advertisement
Amraki

CommContactStatusLog - Complete Module

May 10th, 2015
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'CommunicatorStatusLog
  2.  
  3. 'Excel macro to pull contact status information from Office Communicator
  4.  
  5. 'Works with users not on your contact list
  6. 'Store contact display name & email on sheet 1
  7.     'Use getAllMyContacts to retrieve all of your contacts instead of doing it manually
  8. 'Daily status sheet will be automatically created
  9.  
  10. Option Explicit
  11. Public Sub getAllMyContacts()
  12.      Dim myOC As New CommunicatorAPI.Messenger 'Office Communicator API
  13.     Dim t: Set t = myOC.MyContacts
  14.      Dim i As Integer
  15.      Dim t1 As Variant
  16.    
  17.      Sheet1.Cells(1, 1).Value = "Display Name"
  18.      Sheet1.Cells(1, 2).Value = "Email"
  19.      
  20.      i = 2
  21.      For Each t1 In t
  22.         Sheet1.Cells(i, 1).Value = t1.FriendlyName
  23.         Sheet1.Cells(i, 2).Value = t1.SigninName
  24.         i = i + 1
  25.      Next
  26.      MsgBox "Completed"
  27. End Sub
  28.  
  29. Public Sub Main()
  30.     Dim ContSheet, StatSheet As Worksheet
  31.     Dim lrData, feRow As Long 'lrData= last row of data on contact sheet, feRow= first empty row on current status sheet
  32.    Dim conName, conEmail, currStatus, prevStatus As String
  33.     Dim colToUpdate 'As Long/String??
  34.    Dim boolRepeat, boolExists As Boolean
  35.     Dim i As Integer
  36.  
  37.     'Application.ScreenUpdating = False
  38.  
  39.     Call subAddWorksheet
  40.        
  41.     With ThisWorkbook
  42.         Set ContSheet = .Worksheets(1)
  43.         Set StatSheet = .Worksheets(2)
  44.     End With
  45.    
  46.     lrData = ContSheet.Range("A" & Rows.Count).End(xlUp).Row
  47.     feRow = StatSheet.Range("A" & Rows.Count).End(xlUp).Row + 1
  48.    
  49.     Do
  50.         For i = 2 To lrData
  51.             conName = ContSheet.Range("A" & i).Value
  52.             conEmail = ContSheet.Range("B" & i).Value
  53.             currStatus = funcGetStatus(conEmail)
  54.             Set colToUpdate = StatSheet.Cells.Find(What:=conName, After:=Range("A1"), SearchOrder:=xlByColumns, SearchDirection:=xlPrevious)
  55.             If colToUpdate Is Nothing Then
  56.                 colToUpdate = i 'using i for row number on contact list sheet and column number on status
  57.                                     'sheet maintains consistent order
  58.                boolExists = False
  59.             Else
  60.                 colToUpdate = colToUpdate.Column
  61.                 boolExists = True
  62.             End If
  63.            
  64.             If feRow > 2 Then 'if this isn't the first status of the sheet (row 1 = headers)
  65.                prevStatus = StatSheet.Cells(feRow - 1, i).Value
  66.    
  67.                 If currStatus <> prevStatus And boolRepeat = False Then
  68.                     boolRepeat = True
  69.                     Exit For
  70.                 End If
  71.    
  72.                 If boolRepeat = True Then
  73.                     'add conName to next empty column on row 1 (be sure to insert so sure to not overwrite any info)
  74.                            '^ only if it doesn't already exist (add function to search for it?)
  75.                    'add time to column a (save for end of macro?)
  76.                    'add status to next empty row (feRow)
  77.    
  78.                     Call subLogStatus(conName, currStatus, colToUpdate, feRow, boolExists)
  79.                 End If
  80.             ElseIf feRow = 2 Then
  81.                 'add conName to next empty column on row 1 (be sure to insert so sure to not overwrite any info)
  82.                'add time to column a (save for end of macro?)
  83.                'add status to next empty row (2, duh!)
  84.    
  85.                 Call subLogStatus(conName, currStatus, colToUpdate, feRow, boolExists)
  86.             End If
  87.             If i = lrData And boolRepeat = True Then
  88.                 boolRepeat = False
  89.             End If
  90.         Next i
  91.     Loop Until boolRepeat = False
  92.  
  93.     Application.ScreenUpdating = True
  94.    
  95.     Call subRepeat   'run macro to repeat process on schedule
  96. End Sub
  97.  
  98. Private Function funcGetStatus(ByVal userEmail As String) As String
  99.     Dim myOC As New CommunicatorAPI.Messenger 'Office Communicator API
  100.    Dim curr_status As Integer
  101.     Dim flag As Integer
  102.     Dim stat As String
  103.  
  104.     stat = vbNullString
  105.     curr_status = myOC.GetContact(userEmail, myOC.MyServiceId).Status
  106.     Select Case curr_status
  107.         Case 1
  108.             stat = "Offline"
  109.         Case 2
  110.             stat = "Online"
  111.         Case 6
  112.             stat = "Invisible"
  113.         Case 10
  114.             stat = "Busy"
  115.         Case 14
  116.             stat = "Be Right Back"
  117.         Case 18
  118.             stat = "Inactive"
  119.         Case 34
  120.             stat = "Away"
  121.         Case 50
  122.             stat = "On the Phone"
  123.         Case 66
  124.             stat = "Out to Lunch"
  125.         Case Else
  126.             stat = "Unknown"
  127.     End Select
  128.  
  129.     funcGetStatus = stat
  130. End Function
  131.  
  132. Private Sub subLogStatus(conName, currStatus, colToUpdate, feRow, boolExists)
  133.     If boolExists = False Then
  134.         Cells(1, colToUpdate).EntireColumn.Insert
  135.         Cells(1, colToUpdate).Value = conName
  136.     End If
  137.     Cells(feRow, colToUpdate).Value = currStatus
  138.    
  139.     Range("A" & feRow).Value = Format(Now, "hh:mm am/pm")
  140.     Range("A" & feRow).Select
  141. End Sub
  142.  
  143. Private Sub subAddWorksheet()
  144.     'Insert new sheet to the right of name/email sheet (1)
  145.    'Rename sheet with current date
  146.    Dim SheetName As String
  147.    
  148.     SheetName = Format(Date, "mm-dd-yy")
  149.    
  150.     On Error GoTo AddNew
  151.     ThisWorkbook.Sheets(SheetName).Activate
  152.     Exit Sub
  153.    
  154. AddNew:
  155.     ThisWorkbook.Sheets.Add After:=Worksheets(1)
  156.     ActiveSheet.Name = SheetName
  157. End Sub
  158.  
  159. Private Sub subRepeat()
  160.     Application.OnTime Now + TimeValue("00:00:30"), "Main"
  161. End Sub
  162.  
  163. Private Sub Auto_Open()
  164.     'Run Main Sub
  165.    Main
  166. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement