Newbie: Need to read emails that are stored in a folder w/ VBA

L

lwoods

I am using Outlook 2003.

I want to access a group of emails that are in an Outlook folder. I will be
parsing the contents of the email and either writing them out to a csv file,
or into an Access database.

Can someone either (1) give me an idea how to do this, or (2) point me to a
site where I can get some help?

TIA,

Larry Woods
 
G

Greg J

Hi,

Copy and paste the following function directly into MS Outlook VB
Module.

Function ReadItemsInFolder()

Dim olNms As NameSpace
Dim olFdr As MAPIFolder
Dim olMItem As MailItem
Dim obj As Object
Dim strBody As String

Set olNms = GetNamespace("MAPI")
Set olFdr = olNms.PickFolder

For Each obj In olFdr.Items
'Folders can contain many different item types so you need to
assign each item to
'a generic object then check the class for the item(s) you want
If obj.Class = olMail Then 'Picks only the mail items
Set olMItem = obj
strBody = olMItem.Body
'strBody now contains the message body text. Parse this
variable into your other source (csv, access etc)
End If
Next

Set olMItem = Nothing
Set obj = Nothing
Set olFdr = Nothing
Set olNms = Nothing

End Function

If the VB module is in MS Access, you should:
- Add the Outlook Libarary as a reference
- Precede each Outlook object with Outlook. in the declaration section
and
- Create an Application object

For instance, replace the appropriate lines in the above function with
the following:

Dim olApp as New Outlook.Application 'New line
Dim olNms As Outlook.NameSpace
Dim olFdr As Outlook.MAPIFolder
Dim olMItem as Outlook.MailItem

Set olNms = olApp.GetNamespace("MAPI")

Good luck!


Greg J
 
L

lwoods

Thanks, Greg....

I wanted to acknowledge your reply...and I will try out the code ASAP, but I
have a disk crash right now! (Doesn't it ALWAYS happen when you get
behind?)

I'll get back to you.

Larry Woods
 
L

lwoods

Greg,

It works in debug mode, but when I try to open any of the menu items in the
VB IDE, they are disabled; also all toolbar icons. What am I doing wrong.
I need to add fso in order to write out a csv file.

TIA,

Larry
 
G

Greg J

Hmmm, not sure about why your menu items and toolbars would be
disabled.

Try changing the function to a Sub, that way it will appear in the list
when you click Tools > Macros and you can run the code from Outlook
interface instead of the code module.

As far as FSO goes, link the reference called Windows Script Host
Object Model, or a much simpler way is to use the following code with
existing references. To get extra help with this, position the cursor
on 'Open' and press F1, it should open a page describing the 'Open
Statement'.

Open "e:\test.csv" For Output As #1 'Opens a file ready for input
Print #1, "test1" 'Writes a new line to
the file
Print #1, "test2" 'Writes a new line to
the file
Print #1, "test3" 'Writes a new line to
the file
Close #1 'Closes the file

So to get this code to work you would create a variable that contains
the entire contents of a line then use the Print statement to write the
whole line to the file.

The file called e:\test.csv contains the following:

test1
test2
test3

Hope this helps


Greg J
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top