Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exporting Outlook Email Subjects including date and time 1

Status
Not open for further replies.

PRUSA

Technical User
Sep 23, 2004
35
US
Hello Everyone,

I've done some VBA in excel, and access, but this outlook thing is completely new to me.

I have a particular folder where I have i woudl say 20-40 emails.

All i want to od is export the Mail Subjects, and the Time and Date they were sent, and i guess save to an excel sheet or text file.

I can do the following simply from outlook but it doesn't include data and time.

Does anyone have a workaround for this?

Thanks in advance
 
I've not tried it from Outlook yet but maybe

thread705-1047885

will be helpful, anyway.


TomCologne
 
Well, I gave it a shot. The procedure is rather generic but it works at least in OL2k.


1. Open VBE, Alt + F11
2. Insert>Module
3. Copy & paste this:

Code:
''''''''''''''''''''''''''''''''''''''''''''''
'' Export "Inbox" folder content to *.txt file
''
'' Create OL button:
''
'' 1. View>Toolbars>Customize...>Toolbars>New
'' 2. Enter a name for toolbar
'' 3. Click "Commands" tab
'' 4. Select "Macros" in "Categories"
'' 5. Drag "Project1.ExportMail onto button
'' 6. Optional: Modify Selection
''
''''''''''''''''''''''''''''''''''''''''''''''
Sub ExportMail()
On Error GoTo Err_ExportMail

Dim molNamespace As Outlook.NameSpace, molInbox As Outlook.MAPIFolder, molItem As Outlook.MailItem
Dim i As Integer
Dim strOutput As String

Set molNamespace = Application.GetNamespace("MAPI")
Set molInbox = molNamespace.GetDefaultFolder(olFolderInbox)    
    
    strOutput = "C:\MailItems.txt"

    '' Note: Existing "C:\MailItems.txt" file will be overwritten without warning!
    '' Use strOutput below or something similar for archiving:

    '' strOutput = "C:\MailItems" & Format(Date,"mmddyy") & ".txt"

    Open strOutput For Output As #1    '' Open file for output.
    
    Print #1, "Counter,EntryID,Subject,SentOn"  '' Print header

        For i = 1 To molInbox.Items.Count
                
            Set molItem = molInbox.Items(i)
                                        
                Print #1, i & "," & molItem.EntryID & "," & molItem.Subject & "," & molItem.SentOn
                    
        Next i

Exit_ExportMail:
      
    Set molNamespace = Nothing
    Set molInbox = Nothing
    Set molItem = Nothing
    
    Close #1
    
    Exit Sub

Err_ExportMail:

    Print #1, i & "," & Err.Number & "," & Err.Description
    Resume Next
    
End Sub



Nice detail:

Since it's called from within Outlook the export doesn't bring up the " A program is trying to access..." alert.


TomCologne
 
TomCologne,

This works perfectly!!!
Thanks so much for your help. I really appreciate it.

One question. Since I have so many email in my inbox, lets say i made a new folder how would I point to that?

This is going to save me so much time.

Thanks Again,
Sergio
 
TomCologne,

I was able to find out the answer to this.
One has to create a couple for variables but it works fine.

One can use the Folders method. The trick with this is that you have to go down the line from Highest hierachy to down to the folder you want to open.

I put the code i'm using below:

Set molNamespace = mol.GetNamespace("MAPI")
Set molMailbox = molNamespace.Folders("Mailbox - Example")
Set molInbox = molMailbox.Folders("Inbox")
Set molSubFolder = molInbox.Folders("Backups")


Thanks again for your help. With your help I'll be saving so much time.
 
My pleasure, Sergio.

To refer to an Inbox subfolder, you'd need this line:

Set molInbox = molNamespace.GetDefaultFolder(olFolderInbox).Folders("SubfolderNameXYZ")

This will return items in the specified subfolder only.

TomCologne
 
Oh nice!!

Less coding for that one ;-)

Thanks Again
 
Thanks all. I will give it a go and report back in to tell you if I succeeded.

Sorry to not get back to this until now.
 
Er, Um, disregard my previous post. I thought I was responding to something else. [3eyes]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top