kf dhivya
kf dhivya

Reputation: 85

How to get Outlook Email received time

I need to extract attachments from Emails received in a user preferred time frame.

Say like extract for Emails received between 2PM to 4PM.

Please find the below code I've that extract files perfectly - but it did for all the Emails in the folder.

Please help me to resolve it.

Sub Unzip()

    Dim ns As NameSpace             'variables for the main functionality
    Dim Inbox As MAPIFolder
    Dim SubFolder As MAPIFolder
    Dim Atchmt As Attachment
    Dim FileName As Variant
    Dim msg As Outlook.MailItem


    Dim FSO As Object               'variables for unzipping
    Dim oApp As Object
    Dim FileNameFolder As Variant
    Dim Totalmsg As Object
    Dim oFrom
    Dim oEnd

    Set ns = GetNamespace("MAPI")
    Set Inbox = ns.GetDefaultFolder(olFolderInbox)
    Set SubFolder = Inbox.Folders("TEST")
    Set Totalmsg = msg.ReceivedTime
    oFrom = InputBox("Please give start time", ("Shadowserver report"))
    oEnd = InputBox("Please give End time", ("Shadowserver report"))

   If Totalmsg <= oFrom And Totalmsg >= oEnd Then
   For Each msg In SubFolder.Items
            For Each Atchmt In msg.Attachments
                    If (Right(Atchmt.FileName, 3) = "zip") Then
                    MsgBox "1"

                                    FileNameFolder = "C:\Users\xxxx\Documents\test\"
                                    FileName = FileNameFolder & Atchmt.FileName
                                    Atchmt.SaveAsFile FileName
                                    Set oApp = CreateObject("Shell.Application")
                                    oApp.NameSpace(FileNameFolder).CopyHere oApp.NameSpace(FileName).Items

                                    Kill (FileName)
                                    On Error Resume Next
                                    Set FSO = CreateObject("scripting.filesystemobject")
                                    FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True
                    End If
             Next
    Next
End If
End Sub

Upvotes: 3

Views: 15724

Answers (2)

R3uK
R3uK

Reputation: 14547

Made a few improvements to improve performance and clarity :

  1. Test received time inside the loop on the messages
  2. Defined related variables as Date (like MsG.ReceivedTime) and improved input messages
  3. Added Option Explicit to avoid mishaps in future coding (VERY GOOD PRACTICE)
  4. Use Environ$("USERPROFILE") to get User directory's path
  5. Reorganize variables and initialisation outside of the loops
  6. Added LCase to be sure to get all zips (including .ZIP)

Code :

Option Explicit

Sub Unzip()
    '''Variables for the main functionality
    Dim NS As NameSpace
    Dim InboX As MAPIFolder
    Dim SubFolder As MAPIFolder
    Dim MsG As Outlook.MailItem
    Dim AtcHmt As Attachment
    Dim ReceivedHour As Date
    Dim oFrom As Date
    Dim oEnd As Date
    '''Variables for unzipping
    Dim FSO As Object
    Dim ShellApp As Object
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set ShellApp = CreateObject("Shell.Application")
    Dim FileNameFolder As Variant
    Dim FileName As Variant

    '''Define the Outlook folder you want to scan
    Set NS = GetNamespace("MAPI")
    Set InboX = NS.GetDefaultFolder(olFolderInbox)
    Set SubFolder = InboX.Folders("TEST")

    '''Define the folder where you want to save attachments
    FileNameFolder = Environ$("USERPROFILE") & "\Documents\test\"

    '''Define the hours in between which you want to apply the extraction
    oFrom = CDate(InputBox("Please give Start time" & vbCrLf & _
                            "Example: 9AM", ("Shadowserver report"), "9AM"))
    oEnd = CDate(InputBox("Please give End time" & vbCrLf & _
                            "Example: 6PM", ("Shadowserver report"), "6PM"))

    For Each MsG In SubFolder.items
        ReceivedHour = MsG.ReceivedTime
        If oFrom <= TimeValue(ReceivedHour) And _
            TimeValue(ReceivedHour) <= oEnd Then
            For Each AtcHmt In MsG.Attachments
                FileName = AtcHmt.FileName
                If LCase(Right(FileName, 3)) <> "zip" Then
                Else
                    FileName = FileNameFolder & FileName
                    AtcHmt.SaveAsFile FileName

                    ShellApp.NameSpace(FileNameFolder).CopyHere _
                            ShellApp.NameSpace(FileName).items

                    Kill (FileName)
                    On Error Resume Next
                    FSO.deletefolder Environ$("Temp") & "\Temporary Directory*", True
                End If
            Next AtcHmt
        End If
    Next MsG
End Sub

Upvotes: 3

M--
M--

Reputation: 29153

I am just going to include the part that you need to change. Other lines will be the same. Basically, what you need to do is to set the Totalmsg inside your loop for each msg;

Sub Unzip()

'... copy your code till here

Set SubFolder = Inbox.Folders("TEST")
oFrom = InputBox("Please give start time", ("Shadowserver report"))
oEnd = InputBox("Please give End time", ("Shadowserver report"))


 For Each msg In SubFolder.Items
   Set Totalmsg = msg.ReceivedTime
   If Totalmsg <= oFrom And Totalmsg >= oEnd Then 'You check it for each msg

'rest will be the same until ...

        FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True
     End If
    Next
   End If
 Next

End Sub

Upvotes: 2

Related Questions