VBA作者和发送日期的.msg文件在桌面文件夹

问题描述:

我想写一个宏,通过30k + .msg文件在桌面文件夹和子文件夹中。如果文件名包含“签证流程 - ”或“签署文件 - ”,目标是获得发送日期和作者。另外,这只能在最早的文件中完成。假设我们在一个子文件夹中,并且有三个与“签证流程”有关的文件,那么只有最早的文件才会被考虑。VBA作者和发送日期的.msg文件在桌面文件夹

获取发送日期是我到目前为止所管理的,但我不知道如何实现获取作者。我激活了Outlook加载项,但我是VBA新手,来自互联网的示例代码并不能帮助我掌握有限的知识。

任何帮助,非常感谢!

不幸的是,我不知道如何在这里为您提供一个示例文件,但我很乐意通过电子邮件发送它。

这里我(工作代码)两种类型的电子邮件的发送日期:

'Optimize Macro Speed 
Application.ScreenUpdating = False 
Application.EnableEvents = False 
Application.Calculation = xlCalculationManual 

Dim wb As Workbook 
Dim ws As Worksheet 
Dim FSO As Object, fld As Object, Fil As Object 
Dim fsoFile As Object 
Dim fsoFol As Object 
Dim fsoSubFol As Object 
Dim folderPath As String, subfolderPath As String, folderName As String, FilePath As String 
Dim StepOne As String, StepTwo As String, FileName As String, CompareDate As String 
Dim NextRow As Long 
Dim FindExistingEntry As Range 

Set wb = ActiveWorkbook 
Set ws = wb.Worksheets("Feuil2") 


With ws 
    .UsedRange.Clear 
    .Cells(1, 1).Value = "Main Folder:" 
    .Cells(1, 2).Value = "File Name:" 
    .Cells(1, 3).Value = "MSG Date:" 
    .Cells(1, 4).Value = "File Name:" 
    .Cells(1, 5).Value = "Approved Date:" 
    .Range("A1:E1").Font.Bold = True 
End With 

Application.DisplayAlerts = False 
With Application.FileDialog(msoFileDialogFolderPicker) 
    .AllowMultiSelect = False 
    If .Show <> -1 Then MsgBox "No folder selected! Exiting script.": Exit Sub 
    folderPath = .SelectedItems(1) 
End With 

If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\" 
Set FSO = CreateObject("Scripting.FileSystemObject") 
Set fld = FSO.GetFolder(folderPath) 
If FSO.FolderExists(fld) Then 
    For Each fsoFol In FSO.GetFolder(folderPath).SubFolders 

On Error Resume Next 
      subfolderPath = fsoFol & "\Mails" 

      For Each fsoSubFol In FSO.GetFolder(subfolderPath).Files 


       FilePath = fsoSubFol 
       FileName = Split(FilePath, "\")(4)  'Get only "Visa Process--2017-06-07 15h24m00s.MSG" of target file 4 
       folderName = Split(FilePath, "\")(2) 
       If Mid(FileName, InStrRev(FileName, ".") + 1) = "MSG" Then 

        'Example: Visa Process--2017-06-07 15h24m00s.MSG 
        If InStr(1, FileName, "Visa Process--", vbTextCompare) <> 0 And Left(FileName, 1) = "V" Then 

         NextRow = ws.Cells(ws.Rows.Count, 2).End(xlUp).Row 

         'Example: Visa Process--2017-06-07 15h24m00s.MSG 
         StepOne = Split(FileName, "--")(1) 'No "Visa Process--" 
         StepTwo = Mid(StepOne, 1, 10)  'No Time-Stamp 

         'Make sure to only include the earliest date for each Main Folder "MPCV....." 
         Set FindExistingEntry = ws.Range("A2:A4000").Find(folderName) 

         'If there is already an entry... 
         If Not FindExistingEntry Is Nothing Then 
          CompareDate = ws.Cells(FindExistingEntry.Row, 3).Value 

          'Replace old date for that Main Folder if new date is earlier than previous 
          If DateValue(CompareDate) > DateValue(StepTwo) Then 

           ws.Cells(FindExistingEntry.Row, 2).Value = FileName 
           ws.Cells(FindExistingEntry.Row, 3).Value = DateValue(CompareDate) 

          'Do nothing if Main Folder date is later 
          ElseIf DateValue(CompareDate) < DateValue(StepTwo) Then 

          End If 
         'If there is no entry for the same Main Folder, simply add a new line 
         ElseIf FindExistingEntry Is Nothing Then 

          ws.Cells(NextRow + 1, 1).Value = folderName 
          ws.Cells(NextRow + 1, 2).Value = FileName 
          ws.Cells(NextRow + 1, 3).Value = DateValue(StepTwo) 

         End If 

        End If 

        'Do the same for the second document 
        If InStr(1, FileName, "Document signed--", vbTextCompare) <> 0 And Left(FileName, 1) = "D" Then 

         NextRow = ws.Cells(ws.Rows.Count, 2).End(xlUp).Row 

         'Example: Document signed--2017-06-07 15h24m00s.MSG 
         StepOne = Split(FileName, "--")(1) 'No "Document signed--" 
         StepTwo = Mid(StepOne, 1, 10)  'No Time-Stamp 

         'Make sure to only include the earliest date for each Main Folder "MPCV....." 
         Set FindExistingEntry = ws.Range("A2:A4000").Find(folderName) 

         'If there is already an entry... 
         If Not FindExistingEntry Is Nothing Then 
          CompareDate = ws.Cells(FindExistingEntry.Row, 3).Value 

          'Replace old date for that Main Folder if new date is earlier than previous 
          If DateValue(CompareDate) > DateValue(StepTwo) Then 

           ws.Cells(FindExistingEntry.Row, 4).Value = FileName 
           ws.Cells(FindExistingEntry.Row, 5).Value = DateValue(CompareDate) 

          'Do nothing if Main Folder date is later 
          ElseIf DateValue(CompareDate) < DateValue(StepTwo) Then 

          End If 
         'If there is no entry for the same Main Folder, simply add a new line 
         ElseIf FindExistingEntry Is Nothing Then 

          'ws.Cells(NextRow + 1, 1).Value = folderName 
          'ws.Cells(NextRow, 4).Value = Filename 
          'ws.Cells(NextRow, 5).Value = DateValue(StepTwo) 

         End If 

        End If 
       End If 
      Next 
    Next 
End If 

'Message Box when tasks are completed 
MsgBox "Scan Complete!" 

'Reset Macro Optimization Settings 
Application.EnableEvents = True 
Application.Calculation = xlCalculationAutomatic 
Application.ScreenUpdating = True 
ActiveWorkbook.Saved = True 

创建Outlook.Application对象的实例(进入循环前),从Application.GetNamespace("MAPI")检索Namespace对象,并使用Namespace.OpenSharedItem传递MSG文件的文件na。检索到的MailItem对象将包含如Subject,SenderName,SenderEmailAddress,SentOn等属性。

+0

您好德米特里,非常感谢您的回复。我会尽力实施你的建议!让我在几分钟内回复您 – VBAbeginner

+0

非常感谢您抽出时间。它经过一些调整后才起作用。你有什么想法可以让它变得更快吗?随意编辑上面的代码。 – VBAbeginner