VBA pull Email from oldest to newest

hajiali

Well-known Member
Joined
Sep 8, 2018
Messages
624
Office Version
  1. 2016
Platform
  1. Windows
Using the code below I have achieved to get my email body to excel. However, they are being imported form newest to oldest I need to be able to import form oldest to newest. any help is greatly appreciated.

Thanks :)

VBA Code:
Sub InboxToExcel()
Dim olApp As Outlook.Application
Dim olNS As Namespace
Dim olFolder As MAPIFolder
Dim OutlookMail As Variant
Dim I As Integer
Dim MT As Long
Dim Mnth As String
Dim xMailItem As MailItem
Dim xTable As Word.Table
Dim xDoc As Word.Document
Dim xWb As Workbook
Dim xWs As Worksheet
Dim xRow As Integer
ActiveSheet.Unprotect Password:="password"
On Error Resume Next
Set xWb = ActiveWorkbook
Set xWs = ActiveSheet
xRow = 1
Set olApp = GetOL()
Set olNS = GetNS(olApp)

On Error Resume Next
MT = InputBox("WHAT MONTH BID DO YOU WANT TO IMPORT? TYPE THE MONTH NUMBER 1-JANUARY..ETC")
Mnth = MonthName(MT)
If MT = 0 Then
Exit Sub
Else
Set OutlookApp = New Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(olFolderInbox).Folders("Bids").Folders(Mnth)

I = 1

For Each xMailItem In olFolder.Items
        Set xDoc = xMailItem.GetInspector.WordEditor
        For I = 1 To xDoc.Tables.Count
        Set xTable = xDoc.Tables(I)
        xTable.Range.Copy
        xWs.Range("Y1").Select
        xWs.Paste
        Range("X1").Offset(I, 0).Value = xMailItem.ReceivedTime
        Range("X1").Offset(I, 0).Columns.AutoFit
        Range("X1").Offset(I, 0).VerticalAlignment = xlTop
    Next
    Call EmailCopy
Next
    I = I + 1
   
Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing
Range("X:AH").ClearContents
Call TextToNumbers
End If
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
just got what i needed. just incase anyone needs this I add

VBA Code:
Set olfoder = Items.Sort("[ReceivedTime]", False)

after

Code:
For Each xMailItem In olFolder.Items
 
Upvote 0
sorry but post #2 does only in time order not date and time. does anyone know how to pull this from Oldest to Newest based on date and time?
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,499
Members
449,089
Latest member
Raviguru

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top