A Microsoft Office (Excel, Word) forum. OfficeFrustration

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 » OfficeFrustration forum » Microsoft Outlook » General Discussion
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Code to read emails



 
 
Thread Tools Display Modes
  #1  
Old May 8th, 2010, 11:35 PM posted to microsoft.public.outlook.general
Mark Andrews[_4_]
external usenet poster
 
Posts: 169
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




  #2  
Old May 9th, 2010, 10:09 PM posted to microsoft.public.outlook.general
Milly Staples [MVP - Outlook][_5_]
external usenet poster
 
Posts: 4,690
Default Code to read emails

You may want to repost this to one of the Outlook programming groups as the
developers and coders tend to hang out there moreso than here. I would try
microsoft.public.outlook.program_vba for starters.

--
Milly Staples [MVP - Outlook]

Post all replies to the group to keep the discussion intact.
ALWAYS post your Outlook version.
How to ask a question: http://support.microsoft.com/KB/555375


After furious head scratching, Mark Andrews asked:

| 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


  #3  
Old May 10th, 2010, 02:14 PM posted to microsoft.public.outlook.general
Mark Andrews[_4_]
external usenet poster
 
Posts: 169
Default Code to read emails

Thanks will do.

"Milly Staples [MVP - Outlook]" what@ever wrote in message
...
You may want to repost this to one of the Outlook programming groups as
the
developers and coders tend to hang out there moreso than here. I would
try
microsoft.public.outlook.program_vba for starters.

--
Milly Staples [MVP - Outlook]

Post all replies to the group to keep the discussion intact.
ALWAYS post your Outlook version.
How to ask a question: http://support.microsoft.com/KB/555375


After furious head scratching, Mark Andrews asked:

| 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


 




Thread Tools
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 Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 08:42 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.