Link to home
Start Free TrialLog in
Avatar of ernst8498
ernst8498

asked on

Save email as text file

Using the enclosed code to restrict emails and then save each one as a text file to a directory.  However, it chokes using the activeInspector statement and chokes without it.  I am using Access 97 wiht the Outlook 98 reference library.  What am I missing here.  Addtionally, I will want to delete each message when I am done with it.

The error I get is

Object varibale or With Block variable not set..


Thx...


Public Sub getEmail()

' Dimension varibles
Dim olApp As Outlook.Application
Dim olNameSpace As Outlook.NameSpace
Dim olFolder As MAPIFolder
Dim olRestrictedItems As Outlook.Items
Dim olItem As MailItem
Dim olSaveItem As MailItem

    Set olApp = CreateObject("Outlook.Application")
    Set olNameSpace = olApp.GetNameSpace("MAPI")
    Set olFolder = olNameSpace.GetDefaultFolder(6)
       
       
    Set olRestrictedItems = olFolder.Items.Restrict("[Subject] = ""Test""")
   
    For Each olItem In olRestrictedItems
        'MsgBox olItem.Subject
       
        Set olItem = olApp.ActiveInspector.currentItem
        olItem.SaveAs "C:\My Documents\" & olItem.Subject & Date & Time() & ".txt", olTXT
 

    Next
   
Set olApp = Nothing
Set olNameSpace = Nothing
Set olFolder = Nothing
Set olRestrictedItems = Nothing
Set olItem = Nothing


End Sub
ASKER CERTIFIED SOLUTION
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ernst,

You are including a date in the file name you are saving. That date has illegal characters for a file name. You can't use a forward slash in a file name.

Gary
Avatar of ernst8498
ernst8498

ASKER

This worked - my mistake.  DAAA

Anyway - perhaps you could extend me something else for the 200 pts.  I am looking for a way to recursively loop through all the folders in outlook.  Do you have a code snippet that does this neatly??  Or can you put in the right direction.

Thx.  I will give you the points anyway...
Sure, it would go something like this:

Sub RecurseChildFolders(objFolder as Folder, iLevel as Integer)
dim objChild as Folder

    debug.print tab(ilevel * 4); objFolder.Name
    for each objChild in objFolder.Folders
        RecurseChildFolders(objFolder, iLevel + 1)
    next
end sub


By the way, I charge extra for simplicity :))
Tim,

This did fix my problem.  

I will repost the question for the recursive folder loop and give those points to the person that offered up the simple script for that.  

Thanks to all.