what should I do?
How to export emails from Outlook to Excel automatically?
Normally, you can export the emails from Outlook to Excel with the Import/ Export feature. But, have you ever tried to auto export the emails to Excel file when new messages are arriving? In this article, I will talk about how to export the information of the new incoming messages automatically in Excel.
Auto export email information from outlook to Excel with VBA code
Quickly export emials from Inbox / Sent Items / Drafts...to Excel file:
With Kutools for Outlook's Quick Report feature, you can easily generate an XML file for reporting all items’ information in current email folder or task folder Kutools for Outlook: with more than 40+ handy Excel add-ins, free to try with no limitation in 60 days. Download and try the free trial now!
|
Auto export email information from outlook to Excel with VBA code
To export the new arriving emails to an Excel workbook automatically, please apply the following VBA code:
1. First, you should create a workbook with the below header information as following screenshot shown, so these information of the messages will be exported to this Excel file.
2. Then go to Outlook and hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.
3. In the Microsoft Visual Basic for Applications window, double click ThisOutlookSession from the Project1(VbaProject.OTM) pane to open the mode, and then copy and paste the following code into the blank module.
VBA code: Auto export email information from Outlook to Excel file:
Public WithEvents GMailItems As Outlook.Items Private Sub Application_Startup() Set GMailItems = Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Items End Sub Private Sub GMailItems_ItemAdd(ByVal Item As Object) Dim xMailItem As Outlook.MailItem Dim xExcelFile As String Dim xExcelApp As Excel.Application Dim xWb As Excel.Workbook Dim xWs As Excel.Worksheet Dim xNextEmptyRow As Integer On Error Resume Next If Item.Class <> olMail Then Exit Sub Set xMailItem = Item xExcelFile = "C:\Users\DT168\Desktop\split document\kto-data.xlsx" If IsWorkBookOpen(xExcelFile) = True Then Set xExcelApp = GetObject(, "Excel.Application") Set xWb = GetObject(xExcelFile) If Not xWb Is Nothing Then xWb.Close True Else Set xExcelApp = New Excel.Application End If Set xWb = xExcelApp.Workbooks.Open(xExcelFile) Set xWs = xWb.Sheets(1) xNextEmptyRow = xWs.Range("B" & xWs.Rows.Count).End(xlUp).Row + 1 With xWs .Cells(xNextEmptyRow, 1) = xNextEmptyRow - 1 .Cells(xNextEmptyRow, 2) = xMailItem.SenderName .Cells(xNextEmptyRow, 3) = xMailItem.SenderEmailAddress .Cells(xNextEmptyRow, 4) = xMailItem.Subject .Cells(xNextEmptyRow, 5) = xMailItem.ReceivedTime End With xWs.Columns("A:E").AutoFit xWb.Save End Sub Function IsWorkBookOpen(FileName As String) Dim xFreeFile As Long, xErrNo As Long On Error Resume Next xFreeFile = FreeFile() Open FileName For Input Lock Read As #xFreeFile Close xFreeFile xErrNo = Err On Error GoTo 0 Select Case xErrNo Case 0: IsWorkBookOpen = False Case 70: IsWorkBookOpen = True Case Else: Error xErrNo End Select End Function
Note: In the above code, C:\Users\DT168\Desktop\split document\kto-data.xlsx is the Excel file path and Excel name that you want to locate the exported information, please change it to your need.
4. Still in the Microsoft Visual Basic for Applications window, click Tools > References to go to the References-Project1 dialog box, and check Microsoft Excel Object Library option from the Available References list box, see screenshot:
5. Then click OK button to close the dialog, and then save and close the code window.
6. Now, you should restart the Outlook to make this code take effect. From now on, if there are new emails arriving, they will be exported to the specific workbook automatically, see screenshot:
Kutools for Outlook - Brings 100 Advanced Features to Outlook, and Make Work Much Easier!
- Auto CC/BCC by rules when sending email; Auto Forward Multiple Emails by custom; Auto Reply without exchange server, and more automatic features...
- BCC Warning - show message when you try to reply all if your mail address is in the BCC list; Remind When Missing Attachments, and more remind features...
- Reply (All) With All Attachments in the mail conversation; Reply Many Emails in seconds; Auto Add Greeting when reply; Add Date into subject...
- Attachment Tools: Manage All Attachments in All Mails, Auto Detach, Compress All, Rename All, Save All... Quick Report, Count Selected Mails...
- Powerful Junk Emails by custom; Remove Duplicate Mails and Contacts... Enable you to do smarter, faster and better in Outlook.

