Solved

Save email as text file

Posted on 2001-06-15
5
177 Views
Last Modified: 2010-05-02
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
0
Comment
Question by:ernst8498
  • 2
  • 2
5 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 200 total points
ID: 6195478
You should be able to lose the SetolItem=olApp.Activeinspector line as it doesn't do anything for you.

The problem perhaps is in the date and time suffix to the filename, try using

olItem.SaveAs "c:\My documents\" & olItem.Subject & Format(Now,"yyyy-mm-dd hh_mm_ss") & ".txt",olTxt

In order to avoid having "\" and "." characters in the filename. This may still cause problems if you have characters in the Subject that cannot form part of a filename.
0
 
LVL 5

Expert Comment

by:gbaren
ID: 6195615
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
0
 

Author Comment

by:ernst8498
ID: 6196094
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...
0
 
LVL 5

Expert Comment

by:gbaren
ID: 6196265
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 :))
0
 

Author Comment

by:ernst8498
ID: 6207338
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.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question