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. |
|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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 | |
|
|