Share via

Read outlook E-mails

Anonymous
2013-06-26T17:14:08+00:00

I have some code which works great reading from my inbox. It places portions of the e-mail into a table and it works excellent! However I do not want this to pull the e-mails from my inbox, but I would like it to read the e-mails of another account or Inbox i am connected to. Right now it is going right to my personal Inbox and pulling the e-mails and I need it to go out to another mailbox which is configured in my outlook instead....here is the code I am using and the name of the mailbox is "Mailbox - Region 1 Reporting".

Private Sub but_GetMail_Click()

Dim TempRst As DAO.Recordset

Dim rst As DAO.Recordset

Dim OlApp As Outlook.Application

Dim Inbox As Outlook.MAPIFolder

Dim InboxItems As Outlook.Items

Dim Mailobject As Object

Dim db As DAO.Database

Dim dealer As Integer

DoCmd.SetWarnings False

DoCmd.RunSQL "Delete * from tbl_outlooktemp"

Set db = CurrentDb

Set OlApp = CreateObject("Outlook.Application")

Set Inbox = OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox)

Set TempRst = CurrentDb.OpenRecordset("tbl_OutlookTemp")

'olFolderInbox

Set InboxItems = Inbox.Items

'Inbox.Items

'

For Each Mailobject In InboxItems

If Mailobject.UnRead Then

With TempRst

.AddNew

!Subject = Mailobject.Subject

!from = Mailobject.SenderName

!To = Mailobject.To

!Body = Mailobject.Body

!DateSent = Mailobject.SentOn

.Update

Mailobject.UnRead = False

End With

End If

Next

Set OlApp = Nothing

Set Inbox = Nothing

Set InboxItems = Nothing

Set Mailobject = Nothing

Set TempRst = Nothing

DoCmd.SetWarnings True

End Sub

SOMEONE PLEASE HELP??

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. Anonymous
    2013-07-02T17:38:58+00:00

    I appreciate your input although in the interim I found a much easier solution: i replaced the line that says...

    Set Inbox = OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox)

    with

    Set Inbox = OlApp.GetNamespace("Mapi").Folders("Mailbox - Region 1 Reporting").Folders("Inbox")

    It was a pretty simple solution and this worked perfectly....I hope this helps others who may have the same needs as I do!

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2013-07-02T06:01:04+00:00

    You need to loop through the Outlook Stores, then loop through the folders in the chosen store to locate the folder you want to process e.g.

    Private Inbox As Items

    Private olItem As Outlook.MailItem

    Private oFolder As Outlook.Folder

    Private olStore As Outlook.Store

    Sub Test()

        For Each olStore In olApp.GetNamespace("Mapi").Session.Stores

            'MsgBox Lcase(olStore)

            If LCase(olStore) = "mailbox - region 1 reporting" Then 'use lower case to avoid spelling anomalies

                For Each oFolder In olStore.GetRootFolder.folders

                    'MsgBox olFolder

                    If Lcase(oFolder.Name) = "inbox" Then 'use lower case here also

                        Set Inbox = oFolder.Items

                        'do what you want with the folder items e.g.

                        Set olItem = Inbox.Item(1)

                        MsgBox olItem.Subject

                        '=======================================

                        Exit For

                    End If

                Next oFolder

                Exit For

            End If

        Next olStore

    End Sub

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2013-07-01T23:32:03+00:00

    Create a separate Mail profile of the user you would like to access, using Control Panel -> Mail (32 bit). Then after you create the Outlook object, create a namespace object but for the new profile.

    Dim objNamespace as object

    Set objNS = olApp.GetNamespace("MAPI")

    objNamespace .Logon "", "", False, True

    Here is a detailed link to the Logon method...

    http://msdn.microsoft.com/en-us/library/office/ff861594.aspx

    Was this answer helpful?

    0 comments No comments