I made several changes to the code and it looks as if it is starting to work. However there are several problems:- Speed. Looping through the folders is extremely slow. It would be quicker to read the 'To Do' 'view' in Outlook - if that is even possible. But how?
- I'm not certain that the looping code is working 100% I would appreciate anyone pointing out if they can see any logic flaw in this code
- Not a bug/just haven't coded - If it is not possible to read the To Do view then I need to also pick up the tasks in the Task folder and merge with the mail item tasks.
'/ experiment with reporting on tasks in Outlook
'
'/ before starting:
' copy/paste this code into Outlook (Alt + F11 to open Outlook VBA)
' open folder location Environ("appdata")
' in this location create an empty workbook named "ReportOpenTasks.xlsx"
'/ TO DO
' is the folder looping working correctly now?
' This picks up email messages flagged as tasks, I need to amalgate this code with the tasks in the Tasks folder
' SPEED! This is slow. Ideally should grab data from Outlooks To Do view instead of looping through every subfolder. But is that even possible to grab the data from this view? If it is, how do I find out?
Option Explicit
Option Private Module
Private mxlWbk As Object 'Excel.Workbook
Private mlngRow As Long
Private mavar As Variant
Public Sub ExportTasksAsReport()
Dim mytoplvl As Variant
Dim blnProcessed As Boolean
Dim avar2 As Variant
mlngRow = 2
ReDim mavar(1 To 3, 1 To mlngRow)
mavar(1, 1) = "Desc"
mavar(2, 1) = "Due"
mavar(3, 1) = "Status"
mlngRow = 1
'loop through outlook folders
Set mytoplvl = Outlook.GetNamespace("MAPI").PickFolder.Folders
Do
pLoopFolders mytoplvl, blnProcessed
Loop Until blnProcessed = False
'outlook doesnt have a transpose function?
ReDim avar2(1 To UBound(mavar, 2), 1 To 3)
For mlngRow = LBound(mavar, 2) To UBound(mavar, 2)
avar2(mlngRow, 1) = mavar(1, mlngRow)
avar2(mlngRow, 2) = mavar(2, mlngRow)
avar2(mlngRow, 3) = mavar(3, mlngRow)
Next mlngRow
Call pWriteArrToExistingExcelFile(avar2)
End Sub
Private Sub pWriteArrToExistingExcelFile(ByRef avar2 As Variant)
Dim objExcel As Object
' reference existing workbook. set header for report
Set objExcel = CreateObject("Excel.Application")
Set mxlWbk = objExcel.Workbooks.Open(Environ("appdata") & "\ReportOpenTasks.xlsx")
' With mxlWbk.Sheets("Sheet1")
' .Cells(1, 1) = "Desc"
' .Cells(1, 2) = "Due"
' .Cells(1, 3) = "Status"
' End With
'autofit all column widths then save and close workbook
With mxlWbk.Sheets("Sheet1")
.Cells(1, 1).Resize(UBound(avar2), 3) = avar2
.Columns("A").EntireColumn.AutoFit
.Columns("B").EntireColumn.AutoFit
.Columns("C").EntireColumn.AutoFit
End With
mxlWbk.Save
DoEvents
mxlWbk.Close
Set mxlWbk = Nothing
End Sub
Private Sub pLoopFolders(ByRef mytoplvl As Variant, ByRef blnProcessed As Boolean)
Dim olFdr As Folder
blnProcessed = False
If mytoplvl.Count <> 0 Then
For Each olFdr In mytoplvl
'Debug.Print Format(Now(), "hhmmss") & " GET " & olFdr.Name
If olFdr.Items.Count > 0 Then
pCheckFolderForTasks olFdr
Else
'folder contains no items
blnProcessed = True
End If
If olFdr.Folders.Count > 1 Then
'folder contains sub folders
pLoopFolders olFdr.Folders, blnProcessed
Else
blnProcessed = False
End If
Next olFdr
Else
Debug.Assert False
End If
End Sub
Private Sub pCheckFolderForTasks(ByRef olFdr As Folder)
Dim olItems As Outlook.Items
Dim i As Long
DoEvents 'uncertain
Set olItems = olFdr.Items
For i = 1 To olItems.Count
With olItems(i)
Select Case .Class
Case olMail
If .IsMarkedAsTask Then
' mxlWbk.Sheets("Sheet1").Cells(mlngRow, 1) = .Subject
' mxlWbk.Sheets("Sheet1").Cells(mlngRow, 2) = .TaskDueDate
' mxlWbk.Sheets("Sheet1").Cells(mlngRow, 3) = .FlagStatus 'is this the appropriate equivalent to Task Status?
mlngRow = mlngRow + 1
ReDim Preserve mavar(1 To 3, 1 To mlngRow)
mavar(1, mlngRow) = .Subject
mavar(2, mlngRow) = .TaskDueDate
mavar(3, mlngRow) = .FlagStatus 'is this the appropriate equivalent to Task Status?
End If
Case olTask
'not sure if even possible to have a Task item outside the Task folder? (I am a noob at Outlook VBA)
If Not .Complete Then
' mxlWbk.Sheets("Sheet1").Cells(mlngRow, 1) = .Subject
' mxlWbk.Sheets("Sheet1").Cells(mlngRow, 2) = .DueDate
' mxlWbk.Sheets("Sheet1").Cells(mlngRow, 3) = .Status
mlngRow = mlngRow + 1
ReDim Preserve mavar(1 To 3, 1 To mlngRow)
mavar(1, mlngRow) = .Subject
mavar(2, mlngRow) = .DueDate
mavar(3, mlngRow) = .Status
End If
End Select
End With
Next i
Set olItems = Nothing
End Sub
Bookmarks