我被一个 VBA 脚本困住了,它从我想要的文件夹中的 Outlook 中获取数据,它要求我提供日期,而不是结束日期,然后它最终给了我所有计数他们文件夹中的电子邮件,而不是我需要获取的日期范围。我不擅长脚本,现在卡住了。 任何帮助将不胜感激。
Sub Export_CountOfItems_InEachFolder_toExcel()
Dim xSourceFolder As Outlook.Folder, xSubFolder As Outlook.Folder
Dim xFilePath As String
Dim xExcelApp As Excel.Application
Dim xWb As Excel.Workbook
Dim xWs As Excel.Worksheet
Dim oDate As String
On Error Resume Next
oDate = InputBox("Type the date for count (format YYYY-m-d")
Set xExcelApp = New Excel.Application
Set xWb = xExcelApp.Workbooks.Add
Set xWs = xWb.Sheets(1)
xWs.Cells(1, 1) = "Folder"
xWs.Cells(1, 2) = "Count Items"
Set xSourceFolder = Outlook.Application.Session.PickFolder
If xSourceFolder = nill Then
xWb.Close False
xExcelApp.Quit
Exit Sub
End If
For Each myItem In myItems
dateStr = GetDate(myItem.ReceivedTime)
If dateStr = oDate Then
If Not dict.Exists(dateStr) Then
dict(dateStr) = 0
End If
dict(dateStr) = CLng(dict(dateStr)) + 1
End If
Next myItem
' Output counts per day:
For Each xSubFolder In xSourceFolder.Folders
Call ProcessFolders(xWs, xSubFolder)
Next
xWs.Columns("A:B").AutoFit
Set xShell = CreateObject("Shell.Application")
Set xFolder = xShell.BrowseforFolder(0, "Select a Folder:", 0, 0)
If TypeName(xFolder) = "Nothing" Then
xWb.Close False
xExcelApp.Quit
Exit Sub
End If
Set xFolderItem = xFolder.Self
xFilePath = xFolderItem.Path & "\"
xFilePath = xFilePath & xSourceFolder.Name & "(" & Format(Now, "yyyy-mm-dd hh-mm-ss") & ").xlsx"
xWb.Close True, xFilePath
xExcelApp.Quit
Set xShell = Nothing
MsgBox "Complete!", vbExclamation, "made by Ferdinand"
End Sub
Sub ProcessFolders(ByVal Ws As Worksheet, ByVal xCurFolder As Outlook.Folder)
Dim xSubFld As Folder
Dim xItemCount As Long
Dim xRow As Integer
xItemCount = xCurFolder.Items.Count
xRow = Ws.UsedRange.Rows.Count + 1
Ws.Cells(xRow, 1) = xCurFolder.FolderPath
Ws.Cells(xRow, 2) = xItemCount
If xCurFolder.Folders.Count > 0 Then
For Each xSubFld In xCurFolder.Folders
Call ProcessFolders(Ws, xSubFld)
Next
End If
End Sub
Function GetDate(dt As Date) As String
GetDate = Year(dt) & "-" & Month(dt) & "-" & Day(dt)
End Function