troubleshooting Question

Export emails from Outlook Inbox folder to Excel

Avatar of daintysally
daintysallyFlag for United States of America asked on
OutlookMicrosoft ExcelVB Script
17 Comments1 Solution1172 ViewsLast Modified:
Hi Experts,

I am trying to export emails from the Inbox in Outlook to an existing Excel worksheet.  I was able to modify the code below to export the fields that I need from the Inbox to an Excel spreadsheet, but I need to instead append the records from the Inbox to the next blank row in the existing worksheet.  The worksheet has a header row in the first row and a count column in column A.  I also need to add some code that will populate the count column when a new record is added.  The count is in increments of '1'.   Can someone tell me what I need to add to the code below to make this happen?


Sub Export2Excel()
  On Error GoTo ErrHandler
    Dim appExcel As Excel.Application
    Dim wkb As Excel.Workbook

Dim wks As Excel.Worksheet

Dim rng As Excel.Range

Dim strSheet As String

Dim strPath As String

Dim intRowCounter As Integer

Dim intColumnCounter As Integer

Dim msg As Outlook.MailItem

Dim nms As Outlook.NameSpace

Dim fld As Outlook.MAPIFolder

Dim itm As Object
    strSheet = "Sample.xlsx"
        strPath = "C:\Desktop\"

strSheet = strPath & strSheet

Debug.Print strSheet
    'Select export folder
Set nms = Application.GetNamespace("MAPI")
Set fld = nms.PickFolder
   
'Handle potential errors with Select Folder dialog box.

If fld Is Nothing Then
MsgBox "There are no mail messages to export", vbOKOnly, _
"Error"

Exit Sub

ElseIf fld.DefaultItemType <> olMailItem Then

MsgBox "There are no mail messages to export", vbOKOnly, _
"Error"

Exit Sub

ElseIf fld.Items.Count = 0 Then

MsgBox "There are no mail messages to export", vbOKOnly, _
"Error"

Exit Sub

End If
    'Open and activate Excel workbook.
Set appExcel = CreateObject("Excel.Application")

appExcel.Workbooks.Open (strSheet)

Set wkb = appExcel.ActiveWorkbook

Set wks = wkb.Sheets(1)

wks.Activate

appExcel.Application.Visible = True
   
   
'Copy field items in mail folder.
For Each itm In fld.Items

intColumnCounter = 2

Set msg = itm
intRowCounter = intRowCounter + 2
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = msg.From
intColumnCounter = intColumnCounter + 1
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = msg.Subject
intColumnCounter = intColumnCounter + 1
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = msg.SentOn


Next itm
Set appExcel = Nothing
Set wkb = Nothing
Set wks = Nothing
Set rng = Nothing
Set msg = Nothing
Set nms = Nothing
Set fld = Nothing
Set itm = Nothing
 
Exit Sub

ErrHandler:
If Err.Number = 1004 Then
MsgBox strSheet & " doesn't exist", vbOKOnly, _
"Error"
Else
MsgBox Err.Number & "; Description: ", vbOKOnly, _
"Error"
End If
Set appExcel = Nothing
Set wkb = Nothing
Set wks = Nothing
Set rng = Nothing
Set msg = Nothing
Set nms = Nothing
Set fld = Nothing
Set itm = Nothing
   
End Sub
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 17 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 17 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros