A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Outlook and VBA
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Code to read emails



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old May 10th 10, 03:14 PM posted to microsoft.public.outlook.program_vba
Mark Andrews[_2_]
external usenet poster
 
Posts: 4
Default Code to read emails

I wrote some Microsoft Access 2007 VBA code (see below) to read email
messages from an Outlook folder
and it seems to work fine on my computer.

I have Outlook configured to read from 2 POP/SMTP accounts and the mail
folder name I am passing in is
"personal folders\Inbox\Inquiries"

My client is using Exchange and trying to specify a folder as
"Mailbox-Personsname\Inbox\CRM Updates" and the she can't get the code to
work.

Question has anyone done this for Outlook 2007 configured to read from
Exchange or would you know what I might need to change?
My client is on the other side of the world so I'm finding it difficult to
debug. I don't do this kind of email reading very often.

Note: The client also has two email accounts setup and wants to read from
the non-default one.

Thanks,
Mark

Public Sub ReadMessagesFromMailFolder(MailFolderName As String)
On Error GoTo Err_ReadMessagesFromMailFolder
Dim RS As DAO.Recordset
Dim OlApp As Outlook.Application
Dim Olmapi As Outlook.NameSpace
Dim OlFolderMain As Outlook.MAPIFolder
Dim OlFolder As Outlook.MAPIFolder
Dim olItems As Outlook.Items
Dim Mailobject As Object

'Clear temp table
CurrentDb.Execute ("Delete * from tblOutlookMail")

'Create a connection to outlook
Set OlApp = CreateObject("Outlook.Application")
Set Olmapi = OlApp.GetNamespace("MAPI")

'Open the folder
Set OlFolder = GetFolder(MailFolderName)

'Set up the folders the emails are going to be deposited in
Set olItems = OlFolder.Items

Set RS = CurrentDb.OpenRecordset("tblOutlookMail")

'loop through mail items and add them to table
For Each Mailobject In olItems
With RS
.AddNew
!Subject = Mailobject.Subject
!From = Mailobject.SenderEmailAddress
!To = Mailobject.To
!Body = Mailobject.Body
!DateSent = Mailobject.SentOn
.Update
End With
Next

Exit_ReadMessagesFromMailFolder:
Set OlApp = Nothing
Set Olmapi = Nothing
Set OlFolderMain = Nothing
Set OlFolder = Nothing
Set olItems = Nothing
Set Mailobject = Nothing
Set RS = Nothing
Exit Sub

Err_ReadMessagesFromMailFolder:
MsgBox Err.Description
Resume Exit_ReadMessagesFromMailFolder

End Sub





--
Mark Andrews
RPT Software
http://www.rptsoftware.com
http://www.donationmanagementsoftware.com
Ads
  #2  
Old May 10th 10, 03:49 PM posted to microsoft.public.outlook.program_vba
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default Code to read emails

Instead of coding like that use NameSpace.GetDefaultFolder(olFolderInbox) to
get the Inbox folder. That will work universally. Once you have the Inbox as
a MAPIFolder (Folder in Outlook 2007) you can then use code like this,
assuming oInbox is your Inbox: oInbox.Folders("CRM Updates")

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Mark Andrews" wrote in message
...
I wrote some Microsoft Access 2007 VBA code (see below) to read email
messages from an Outlook folder
and it seems to work fine on my computer.

I have Outlook configured to read from 2 POP/SMTP accounts and the mail
folder name I am passing in is
"personal folders\Inbox\Inquiries"

My client is using Exchange and trying to specify a folder as
"Mailbox-Personsname\Inbox\CRM Updates" and the she can't get the code to
work.

Question has anyone done this for Outlook 2007 configured to read from
Exchange or would you know what I might need to change?
My client is on the other side of the world so I'm finding it difficult to
debug. I don't do this kind of email reading very often.

Note: The client also has two email accounts setup and wants to read from
the non-default one.

Thanks,
Mark

Public Sub ReadMessagesFromMailFolder(MailFolderName As String)
On Error GoTo Err_ReadMessagesFromMailFolder
Dim RS As DAO.Recordset
Dim OlApp As Outlook.Application
Dim Olmapi As Outlook.NameSpace
Dim OlFolderMain As Outlook.MAPIFolder
Dim OlFolder As Outlook.MAPIFolder
Dim olItems As Outlook.Items
Dim Mailobject As Object

'Clear temp table
CurrentDb.Execute ("Delete * from tblOutlookMail")

'Create a connection to outlook
Set OlApp = CreateObject("Outlook.Application")
Set Olmapi = OlApp.GetNamespace("MAPI")

'Open the folder
Set OlFolder = GetFolder(MailFolderName)

'Set up the folders the emails are going to be deposited in
Set olItems = OlFolder.Items

Set RS = CurrentDb.OpenRecordset("tblOutlookMail")

'loop through mail items and add them to table
For Each Mailobject In olItems
With RS
.AddNew
!Subject = Mailobject.Subject
!From = Mailobject.SenderEmailAddress
!To = Mailobject.To
!Body = Mailobject.Body
!DateSent = Mailobject.SentOn
.Update
End With
Next

Exit_ReadMessagesFromMailFolder:
Set OlApp = Nothing
Set Olmapi = Nothing
Set OlFolderMain = Nothing
Set OlFolder = Nothing
Set olItems = Nothing
Set Mailobject = Nothing
Set RS = Nothing
Exit Sub

Err_ReadMessagesFromMailFolder:
MsgBox Err.Description
Resume Exit_ReadMessagesFromMailFolder

End Sub





--
Mark Andrews
RPT Software
http://www.rptsoftware.com
http://www.donationmanagementsoftware.com


  #3  
Old May 10th 10, 04:26 PM posted to microsoft.public.outlook.program_vba
Mark Andrews[_2_]
external usenet poster
 
Posts: 4
Default Code to read emails

Ken,

I had code like that and it did work. However I couldn't figure out how to
make it work for the folder the client wanted to use.
The client is very particular that it has to be the folder she wants.

She has 2 exchange accounts setup and if you look at her folders she has
- MailBox - XXX
Inbox is under this

- Mailbox - YYY
Inbox is under this

Let's assume XXX is the default mailbox, the code you indicate would goto
the Inbox of XXX and then you can reference subfolders.

How would I reference a folder under Mailbox - YYY?

Thanks,
Mark




"Ken Slovak - [MVP - Outlook]" wrote in message
...
Instead of coding like that use NameSpace.GetDefaultFolder(olFolderInbox)
to get the Inbox folder. That will work universally. Once you have the
Inbox as a MAPIFolder (Folder in Outlook 2007) you can then use code like
this, assuming oInbox is your Inbox: oInbox.Folders("CRM Updates")

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Mark Andrews" wrote in message
...
I wrote some Microsoft Access 2007 VBA code (see below) to read email
messages from an Outlook folder
and it seems to work fine on my computer.

I have Outlook configured to read from 2 POP/SMTP accounts and the mail
folder name I am passing in is
"personal folders\Inbox\Inquiries"

My client is using Exchange and trying to specify a folder as
"Mailbox-Personsname\Inbox\CRM Updates" and the she can't get the code to
work.

Question has anyone done this for Outlook 2007 configured to read from
Exchange or would you know what I might need to change?
My client is on the other side of the world so I'm finding it difficult
to
debug. I don't do this kind of email reading very often.

Note: The client also has two email accounts setup and wants to read from
the non-default one.

Thanks,
Mark

Public Sub ReadMessagesFromMailFolder(MailFolderName As String)
On Error GoTo Err_ReadMessagesFromMailFolder
Dim RS As DAO.Recordset
Dim OlApp As Outlook.Application
Dim Olmapi As Outlook.NameSpace
Dim OlFolderMain As Outlook.MAPIFolder
Dim OlFolder As Outlook.MAPIFolder
Dim olItems As Outlook.Items
Dim Mailobject As Object

'Clear temp table
CurrentDb.Execute ("Delete * from tblOutlookMail")

'Create a connection to outlook
Set OlApp = CreateObject("Outlook.Application")
Set Olmapi = OlApp.GetNamespace("MAPI")

'Open the folder
Set OlFolder = GetFolder(MailFolderName)

'Set up the folders the emails are going to be deposited in
Set olItems = OlFolder.Items

Set RS = CurrentDb.OpenRecordset("tblOutlookMail")

'loop through mail items and add them to table
For Each Mailobject In olItems
With RS
.AddNew
!Subject = Mailobject.Subject
!From = Mailobject.SenderEmailAddress
!To = Mailobject.To
!Body = Mailobject.Body
!DateSent = Mailobject.SentOn
.Update
End With
Next

Exit_ReadMessagesFromMailFolder:
Set OlApp = Nothing
Set Olmapi = Nothing
Set OlFolderMain = Nothing
Set OlFolder = Nothing
Set olItems = Nothing
Set Mailobject = Nothing
Set RS = Nothing
Exit Sub

Err_ReadMessagesFromMailFolder:
MsgBox Err.Description
Resume Exit_ReadMessagesFromMailFolder

End Sub





--
Mark Andrews
RPT Software
http://www.rptsoftware.com
http://www.donationmanagementsoftware.com


  #4  
Old May 10th 10, 05:18 PM posted to microsoft.public.outlook.program_vba
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default Code to read emails

The root folder of the mailbox would be Inbox.Parent. Just get Inbox and the
Parent property will point to the root folder (Outlook Today).

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Mark Andrews" wrote in message
...
Ken,

I had code like that and it did work. However I couldn't figure out how
to make it work for the folder the client wanted to use.
The client is very particular that it has to be the folder she wants.

She has 2 exchange accounts setup and if you look at her folders she has
- MailBox - XXX
Inbox is under this

- Mailbox - YYY
Inbox is under this

Let's assume XXX is the default mailbox, the code you indicate would goto
the Inbox of XXX and then you can reference subfolders.

How would I reference a folder under Mailbox - YYY?

Thanks,
Mark


  #5  
Old May 10th 10, 07:16 PM posted to microsoft.public.outlook.program_vba
Mark Andrews[_2_]
external usenet poster
 
Posts: 4
Default Code to read emails

I'm sorry I still don't understand. She provided a screenshot which shows
three trees of folders.
the top three folders show:
- MailBox - XXX
- Mailbox - YYY
- Archive Folders

so Inbox.Parent would be MailBox - XXX

How do I retrieve folder "Mailbox - YYY\Inbox\CRMUpdates"?

Do I have to traverse two parents and then "Mailbox - YYY" and then "inbox"
and then "CRM Updates"?

In my Outlook I have "personal Folders\Inbox" so your logic makes sense for
my situation.

Thanks,
Mark





"Ken Slovak - [MVP - Outlook]" wrote in message
...
The root folder of the mailbox would be Inbox.Parent. Just get Inbox and
the Parent property will point to the root folder (Outlook Today).

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Mark Andrews" wrote in message
...
Ken,

I had code like that and it did work. However I couldn't figure out how
to make it work for the folder the client wanted to use.
The client is very particular that it has to be the folder she wants.

She has 2 exchange accounts setup and if you look at her folders she has
- MailBox - XXX
Inbox is under this

- Mailbox - YYY
Inbox is under this

Let's assume XXX is the default mailbox, the code you indicate would goto
the Inbox of XXX and then you can reference subfolders.

How would I reference a folder under Mailbox - YYY?

Thanks,
Mark


  #6  
Old May 10th 10, 09:04 PM posted to microsoft.public.outlook.program_vba
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default Code to read emails

NameSpace.Folders has all the folders and stores. Each mailbox is a store,
as is a PST file (Personal Folders). If 3 stores are loaded (not counting
any public folders) NameSpace.Folders(1) would be one of them, etc. The
first one may not be the default though.

For each folder under a store you'd use a construct like Folder.Folders() to
get at subfolders.

Inbox.Parent would only apply to the default store. GetDefaultFolder() only
works for getting a default folder from the default store (where email is
delivered for the logged in user).

In a case such as you have you'd need to know the exact name and location of
every store and folder of interest. That hard coding of names would make it
impossible to make your code universal, you'd need to customize it for each
person and every variation of loaded stores, folders and folder names.
That's why almost all developers will fix names and folder locations.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Mark Andrews" wrote in message
...
I'm sorry I still don't understand. She provided a screenshot which shows
three trees of folders.
the top three folders show:
- MailBox - XXX
- Mailbox - YYY
- Archive Folders

so Inbox.Parent would be MailBox - XXX

How do I retrieve folder "Mailbox - YYY\Inbox\CRMUpdates"?

Do I have to traverse two parents and then "Mailbox - YYY" and then
"inbox" and then "CRM Updates"?

In my Outlook I have "personal Folders\Inbox" so your logic makes sense
for my situation.

Thanks,
Mark


  #7  
Old May 10th 10, 10:52 PM posted to microsoft.public.outlook.program_vba
Mark Andrews[_2_]
external usenet poster
 
Posts: 4
Default Code to read emails

Thanks Ken that makes sense! Now I just need to decide what I should do.

Mark

"Ken Slovak - [MVP - Outlook]" wrote in message
...
NameSpace.Folders has all the folders and stores. Each mailbox is a store,
as is a PST file (Personal Folders). If 3 stores are loaded (not counting
any public folders) NameSpace.Folders(1) would be one of them, etc. The
first one may not be the default though.

For each folder under a store you'd use a construct like Folder.Folders()
to get at subfolders.

Inbox.Parent would only apply to the default store. GetDefaultFolder()
only works for getting a default folder from the default store (where
email is delivered for the logged in user).

In a case such as you have you'd need to know the exact name and location
of every store and folder of interest. That hard coding of names would
make it impossible to make your code universal, you'd need to customize it
for each person and every variation of loaded stores, folders and folder
names. That's why almost all developers will fix names and folder
locations.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Mark Andrews" wrote in message
...
I'm sorry I still don't understand. She provided a screenshot which
shows three trees of folders.
the top three folders show:
- MailBox - XXX
- Mailbox - YYY
- Archive Folders

so Inbox.Parent would be MailBox - XXX

How do I retrieve folder "Mailbox - YYY\Inbox\CRMUpdates"?

Do I have to traverse two parents and then "Mailbox - YYY" and then
"inbox" and then "CRM Updates"?

In my Outlook I have "personal Folders\Inbox" so your logic makes sense
for my situation.

Thanks,
Mark


 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need code snippet to read offline PST file ramserp Outlook and VBA 1 March 22nd 10 02:15 PM
Emails automatically being marked as Read... Mike Outlook - General Queries 4 August 15th 07 03:52 PM
Restored emails only display html code, How to read correctly? simon Outlook - General Queries 0 June 8th 07 01:09 PM
Can't read some emails [email protected] Outlook - General Queries 1 June 8th 07 02:16 AM
Code for read the Numeric which is in detween the words.. safs Outlook - Using Contacts 2 October 12th 06 03:12 PM


All times are GMT +1. The time now is 08:57 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.