Reading Outlook mail

M

Mark Andrews

I wrote some 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
 
D

David Lloyd

Mark:

I don't have all the answers to your post at this time, however, I can offer
a couple suggestions.

First,y ou may want to cross post to the Outlook newsgroups, if you have not
already, since this is very much an Outlook related question.

That being said, my recollection is that Exchange Server accounts don't use
the same folder naming convention as regular Outlook accounts. I tried
something similar awhile back and my recollection is that the Exchange
folders are referenced through Active Directory containers, or something to
that effect. Therefore the normal "Mailbox-Personsname\Inbox\CRM Updates"
does not work.

When the Exchange account is the default account you can use the
GetDefaultFolder method of the Namespace object to reference certain default
folders and then drill down from there using regular folder names. For
example,

Set fldCRMUpdates = nms.GetDefaultFolder(olFolderInbox).Folders("CRM
Updates")

This gets you around the direct reference issue related to Exchange folders.

Below is a link to some reference material on the Namespace object methods:

http://support.microsoft.com/default.aspx/kb/310244?p=1

You seem to imply, but I'm not certain, that your client's non-default email
account is an Exchange Server account. Could you please confirm this?

If I find additional information, I will pass it along. I'm sure the
Outlook folks will have much more to say on these issues.

David Lloyd
Lemington Consulting
http://lemingtonit.com
 
M

Mark Andrews

David,

Thanks for the info! I originally built the code to work off the default
Inbox folder (as you suggested) and then referenced
a subfolder. This did work. However the user is very particular and wants
the folder to be on the NON-default exchange account.
Thus my attempt to just allow the user to specific any folder and supply the
full folder path. Works fine for me but difficult for me to
simulate having an Exchange account.

If you find any additional information I would be very grateful if you add
to this thread.

I will also try posting to the Outlook newsgroup.

Thanks,
Mark
 
A

Arvin Meyer [MVP]

I don't think this is exactly what you are looking for, but it does show how
to address the MAPI namespace. I wrote this code maybe 7 or 8 years ago. I
haven't looked at it in at least 6 years, so I can't really remember much
about it. I used it to refresh an Access table with items from a contact
folder on an Exchange Server. It was not the default folder, but it was a
folder inside the All Public Folders:

Dim olApp As Object
Dim nsMAPI As NameSpace
Dim CFolder As MAPIFolder

Set olApp = GetObject("", "Outlook.Application")
Set nsMAPI = olApp.GetNamespace("MAPI")
Set CFolder = nsMAPI.Folders("Public Folders").Folders("All Public
Folders").Folders("Contractors")
Set Contractors = CFolder.Items.Restrict("[MessageClass] =
'IPM.Contact.Contractor'")

HTH
 
M

Mark Andrews

Arvin,

Thanks I can give this a try when my client gets back from vacation.

Mark

Arvin Meyer said:
I don't think this is exactly what you are looking for, but it does show
how to address the MAPI namespace. I wrote this code maybe 7 or 8 years
ago. I haven't looked at it in at least 6 years, so I can't really
remember much about it. I used it to refresh an Access table with items
from a contact folder on an Exchange Server. It was not the default
folder, but it was a folder inside the All Public Folders:

Dim olApp As Object
Dim nsMAPI As NameSpace
Dim CFolder As MAPIFolder

Set olApp = GetObject("", "Outlook.Application")
Set nsMAPI = olApp.GetNamespace("MAPI")
Set CFolder = nsMAPI.Folders("Public Folders").Folders("All Public
Folders").Folders("Contractors")
Set Contractors = CFolder.Items.Restrict("[MessageClass] =
'IPM.Contact.Contractor'")

HTH
--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.accessmvp.com
http://www.mvps.org/access

Mark Andrews said:
David,

Thanks for the info! I originally built the code to work off the default
Inbox folder (as you suggested) and then referenced
a subfolder. This did work. However the user is very particular and
wants the folder to be on the NON-default exchange account.
Thus my attempt to just allow the user to specific any folder and supply
the full folder path. Works fine for me but difficult for me to
simulate having an Exchange account.

If you find any additional information I would be very grateful if you
add to this thread.

I will also try posting to the Outlook newsgroup.

Thanks,
Mark
 

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