Consulting

Results 1 to 8 of 8

Thread: Solved: Export senders email address and subject line to Excel

  1. #1
    VBAX Regular
    Joined
    Aug 2009
    Posts
    44
    Location

    Solved: Export senders email address and subject line to Excel

    Does anyone know of a vba code that will enable me to export the senders email address and subject line from a particular folder to excel?!

    I'm currently using Outlook 2002.

    I have tried incorporating the following code, however to no avail:

    [VBA]
    Sub ExportToExcel()
    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 = "OutlookItems.xls"
    strPath = "P:\Group Reward\Team\Sam\Outlook Items"
    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 = 1
    Set msg = itm
    intRowCounter = intRowCounter + 1
    Set rng = wks.Cells(intRowCounter, intColumnCounter)
    rng.Value = msg.To
    intColumnCounter = intColumnCounter + 1
    Set rng = wks.Cells(intRowCounter, intColumnCounter)
    rng.Value = msg.SenderEmailAddress
    intColumnCounter = intColumnCounter + 1
    Set rng = wks.Cells(intRowCounter, intColumnCounter)
    rng.Value = msg.Subject

    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
    [/VBA]

    Any help would be much appreciated.

    Thanks.

  2. #2
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    Can you tell me what happens when you run the code? I ran it and I got the Name and subject from it.

  3. #3
    VBAX Regular
    Joined
    Aug 2009
    Posts
    44
    Location
    I keep receiving a compile error on the following line:

    [VBA]
    Dim appExcel As Excel.Application
    [/VBA]

    User defined type - not defined.

  4. #4
    VBAX Regular
    Joined
    Aug 2009
    Posts
    44
    Location
    Problem solved. I failed to select the MS Excel object from the reference listing. Lol!!!

    Nevertheless JKwan, I appreciate your help.

  5. #5
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    You might consider adding a function that verifies that the path and filename exist, before attempting to open the workbook.

    Also, if you're planning on running this code more than once, your code as written will overwrite the existing data on the worksheet.

    Turning off screen updating might make the code run a bit faster:

    [VBA]
    appExcel.ScreenUpdating = False
    [/VBA]

  6. #6
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    262
    Location
    Quote Originally Posted by SDave
    Problem solved. I failed to select the MS Excel object from the reference listing. Lol!!!

    Nevertheless JKwan, I appreciate your help.

    I am getting the same error, how would I do the same?

    ps. I have outlook 2003

  7. #7
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    Quote Originally Posted by sassora
    I am getting the same error, how would I do the same?

    ps. I have outlook 2003
    Well, did you add a reference to your code?
    In Outlook - Tools - Reference and select "Microsoft Excel"

  8. #8
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    262
    Location
    Excellent, thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •