Solved

Save email as text file

Posted on 2001-06-15
5
175 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now