У меня был этот код, сидящий в течение нескольких лет - возможно, он нуждается в настройке.
Вам нужно будет создать книгу и дать лист кодовому имени shtAnalysis
.
Добавьте этот код в нормальный модуль в книгу и выполните процедуру CreateReport
.
Public Sub CreateReport()
Dim oOutlook As Object 'Outlook.Application
Dim nNameSpace As Object 'Outlook.Namespace
Dim mFolderSelected As Object 'Outlook.MAPIFolder
Dim oItem As Object
Dim rLastCell As Range
Dim x As Long
'Solves the "Code execution has been interrupted" problem.
Application.EnableCancelKey = xlDisabled
Application.EnableCancelKey = xlInterrupt
Set oOutlook = GetObject(, "Outlook.Application")
Set nNameSpace = oOutlook.GetNamespace("MAPI")
Set mFolderSelected = nNameSpace.PickFolder
shtAnalysis.Cells.Delete Shift:=xlUp
ProcessFolder mFolderSelected
Set rLastCell = LastCell(shtAnalysis)
With shtAnalysis
.Columns.ColumnWidth = 100
.Cells.EntireColumn.AutoFit
.Range(.Cells(1, 1), .Cells(rLastCell.Row, rLastCell.Column)).Sort _
Key1:=.Range("A2"), Order1:=xlDescending, Header:=xlYes
'Add totals to row 1 & column A.
.Rows("1:1").Insert Shift:=xlDown
.Columns("A:A").Insert Shift:=xlToRight
For x = 3 To rLastCell.Column
With .Cells(1, x)
.FormulaR1C1 = "=SUM(R3C:R" & rLastCell.Row & "C)"
.NumberFormat = "General"
.Font.Bold = True
.Font.ColorIndex = 3
End With
Next x
For x = 3 To rLastCell.Row
With .Cells(x, 1)
.FormulaR1C1 = "=SUM(RC3:RC" & rLastCell.Column & ")"
.NumberFormat = "General"
.Font.Bold = True
.Font.ColorIndex = 3
End With
Next x
'Add grand total.
With .Cells(1, 1)
.FormulaR1C1 = "=SUM(RC3:RC" & rLastCell.Column & ")"
.NumberFormat = "General"
.Font.Bold = True
.Font.Size = 14
.Font.ColorIndex = 3
End With
End With
ThisWorkbook.Activate
MsgBox "Complete", vbOKOnly
End Sub
Private Sub ProcessFolder(oParent As Object)
Dim oFolder As Object 'Outlook.MAPIFolder
Dim oMail As Object
Dim sName As String
On Error Resume Next
For Each oMail In oParent.Items
PlaceDetails Int(oMail.SentOn), oParent
Next oMail
If (oParent.Folders.Count > 0) Then
For Each oFolder In oParent.Folders
ProcessFolder oFolder
Next oFolder
End If
On Error GoTo 0
End Sub
Public Sub PlaceDetails(dDate As Date, oFolders As Object)
Dim rFoundCell As Range
Dim lRow As Long, lColumn As Long
Dim sItem As String
Dim lLevel As Long
Dim x As Long
sItem = oFolders.FullFolderPath 'User the full path of the folder.
If Left(sItem, "2") = "\\" Then
sItem = Mid(sItem, 3, Len(sItem)) 'Remove leading backslashes.
End If
lLevel = Len(sItem) - Len(Replace(sItem, "\", ""))
For x = 1 To lLevel
sItem = Left(sItem, InStr(sItem, "\") - 1) & Replace(sItem, "\", Chr(10) & Application.WorksheetFunction.Rept(" ", x) & Chr(149), InStr(sItem, "\"), 1)
Next x
With shtAnalysis
.Columns(1).EntireColumn.AutoFit
'First find the column by looking for sItem in row 1.
Set rFoundCell = .Rows("1:1").Cells.Find(What:=sItem, After:=.Cells(1, 1), LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlNext)
If Not rFoundCell Is Nothing Then
lColumn = rFoundCell.Column
Else
lColumn = LastCell(shtAnalysis).Column + 1
End If
Set rFoundCell = Nothing
'Next find the row by looking for dDate in column A.
Set rFoundCell = .Columns("A:A").Cells.Find(What:=dDate, After:=.Cells(2, 1), LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlNext)
If Not rFoundCell Is Nothing Then
lRow = rFoundCell.Row
Else
lRow = LastCell(shtAnalysis).Row + 1
End If
Set rFoundCell = Nothing
'Place the data.
.Cells(lRow, 1).Value = dDate
.Cells(1, lColumn).Value = sItem
If .Cells(lRow, lColumn) = "" Then
.Cells(lRow, lColumn).NumberFormat = "General"
.Cells(lRow, lColumn) = 1
Else
.Cells(lRow, lColumn) = .Cells(lRow, lColumn) + 1
End If
End With
End Sub
' Purpose : Finds the last cell containing data or a formula within the given worksheet.
' If the Optional Col is passed it finds the last row for a specific column.
'---------------------------------------------------------------------------------------
Public Function LastCell(wrkSht As Worksheet, Optional Col As Long = 0) As Range
Dim lLastCol As Long, lLastRow As Long
On Error Resume Next
With wrkSht
If Col = 0 Then
lLastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
lLastRow = .Cells.Find("*", , , , xlByRows, xlPrevious).Row
Else
lLastCol = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column
lLastRow = .Columns(Col).Find("*", , , , xlByColumns, xlPrevious).Row
End If
If lLastCol = 0 Then lLastCol = 1
If lLastRow = 0 Then lLastRow = 1
Set LastCell = wrkSht.Cells(lLastRow, lLastCol)
End With
On Error GoTo 0
End Function
Это даст результат, как:
Будет ли «objFolder.Items.Count» содержать элементы календаря, элементы задач и т. Д. Это не будет тот же счет, что и только элементы электронной почты. –