Solved

Save email as text file

Posted on 2001-06-15
5
179 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

749 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