Solved

Downloading Emails from mailbox other than default to MS Access using VBA

Posted on 2011-03-18
14
862 Views
Last Modified: 2012-05-11
Hello

Hoping someone can advise. I have code to download emails from a mailbox to an Access database. It works fine against the default mailbox, but I have now mapped my Outlook to another mailbox and it is from this that I want to download the emails. Currently my code is as below. Please can someone advise what syntax I have to use to force the code to download the emails from a mailbox other than the default one.

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

Thanks in advance.


0
Comment
Question by:Begbie76
  • 9
  • 5
14 Comments
 
LVL 17

Expert Comment

by:Barry Cunney
ID: 35164631
I am thinking that if you have mapped your Outlook to a new mailbox the above line of code should work, as it should reference the Inbox folder of the mailbox that you mapped Outlook to.

Otherwise in your code prior to this you may have to specify in the code for the MAPI session to explicitly connect to this other mailbox
0
 
LVL 17

Expert Comment

by:Barry Cunney
ID: 35164681
This is the logon methd
objSession.Logon( [profileName] [, profilePassword] [, showDialog] [, newSession] [, parentWindow] [, NoMail] [, ProfileInfo] )

So for initial testing of your set you could do something like this
Set oSession = CreateObject("MAPI.Session")
oSession.Logon <yourmailprofilename>, <yourpassword>, True

by setting the [showDialog] to true you shoukd get a logon dialog and this may help you to confirm that the session is actually logging on to the other mailbox.

0
 

Author Comment

by:Begbie76
ID: 35164708
Thanks for your quick response. I'm mapped to two mailboxes in Outlook, my personal one and this group mailbox, and it seems to be using my personal one as the default. I only want to download emails from the group one. I'm specifying the MAPI session like this:

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

The group mailbox is called 'OCC-CardAdmin'.
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 17

Expert Comment

by:Barry Cunney
ID: 35164965
This is a longshot - try using GetSharedDefaultFolder method

Sub ResolveName()
    Dim myNamespace As Outlook.NameSpace
    Dim myRecipient As Outlook.Recipient
    Dim CalendarFolder As Outlook.Folder
      
    Set myNamespace = Application.GetNamespace("MAPI")
    Set myRecipient = myNamespace.CreateRecipient("<your group name>")
    myRecipient.Resolve
    If myRecipient.Resolved Then
        Call ShowInbox(myNamespace, myRecipient)
    End If
End Sub

Sub ShowInbox(myNamespace, myRecipient)
    Dim InboxFolder As Outlook.Folder

    Set InboxFolder = _
        myNamespace.GetSharedDefaultFolder _
        (myRecipient, olFolderCalendar)
    InboxFolder.Display
End Sub

0
 
LVL 17

Expert Comment

by:Barry Cunney
ID: 35164971
small update to code
Sub ResolveName()
    Dim myNamespace As Outlook.NameSpace
    Dim myRecipient As Outlook.Recipient
    Dim CalendarFolder As Outlook.Folder
     
    Set myNamespace = Application.GetNamespace("MAPI")
    Set myRecipient = myNamespace.CreateRecipient("<your group name>")
    myRecipient.Resolve
    If myRecipient.Resolved Then
        Call ShowInbox(myNamespace, myRecipient)
    End If
End Sub

Sub ShowInbox(myNamespace, myRecipient)
    Dim InboxFolder As Outlook.Folder

    Set InboxFolder = _
        myNamespace.GetSharedDefaultFolder _
        (myRecipient, olFolderInbox)
    InboxFolder.Display
End Sub

0
 

Author Comment

by:Begbie76
ID: 35165291
Just tried that. I'm getting a 'Method or data member not found' error at .GetNamespace.
0
 
LVL 17

Expert Comment

by:Barry Cunney
ID: 35165490
Try this - put this code in one sub behind a button to test

Sub Display_Inbox_Click()
Dim OlApp As Outlook.Application
Dim myNamespace As Outlook.NameSpace

Dim myRecipient As Outlook.Recipient
Dim Inbox As Outlook.Folder
     
Set OlApp = New Outlook.Application
     
Set myNamespace = OlApp.GetNamespace("MAPI")
Set myRecipient = myNamespace.CreateRecipient("'OCC-CardAdmin'")
   
myRecipient.Resolve
    If myRecipient.Resolved Then
        Set Inbox = myNamespace.GetSharedDefaultFolder(myRecipient, olFolderInbox)
   
        Inbox.Display

    End If

End Sub
0
 

Author Comment

by:Begbie76
ID: 35165573
Just tried that. On the If statement myRecipient isn't resolved, so it's just jumping to End If.
0
 
LVL 17

Expert Comment

by:Barry Cunney
ID: 35165581
Is it MS Outlook which is your e-mail application?
0
 
LVL 17

Expert Comment

by:Barry Cunney
ID: 35165651
What is displayed for the other mailbox in your MS Outlook, similar to the print screen I have attached?
MailBoxPrintScreen.jpg
0
 
LVL 17

Accepted Solution

by:
Barry Cunney earned 500 total points
ID: 35165774
You could also give this a shot

Dim OlApp As Outlook.Application
Dim myNamespace As Outlook.NameSpace

Dim myRecipient As Outlook.Recipient
Dim Inbox As Outlook.Folder
     
Set OlApp = New Outlook.Application
     
Set myNamespace = OlApp.GetNamespace("MAPI")

Set Inbox = myNamespace.Folders("Mailbox - OCC-CardAdmin").Folders("Inbox")

    Inbox.Display
0
 

Author Comment

by:Begbie76
ID: 35166218
It is MS Outlook...I've attached a screenshot (sorry, don't have the Java installed for a screencast). Access is taking it from the Abdey Mark mailbox instead of the OCC-CardAdmin. I'll give your code above a whirl. Thanks for your help, it's most appreciated.

 Doc1.doc
0
 
LVL 17

Expert Comment

by:Barry Cunney
ID: 35166323
The last code I posted should work and should reference the OCC-CardAdmin mailbox as displayed in your print screen

The last line
Inbox.Display
is just a line to test that the 'InBox' variable(folder object) got a valid reference to the Inbox in the OCC-CardAdmin Mailbox
It should display in a new popupOutlook window the Inbox of OCC-CardAdmin mailbox

Then you will know that Inbox references the required inbox  and you can then proceed to do with this whatever code you were doing before to download the mails
0
 

Author Comment

by:Begbie76
ID: 35166589
Thank you so much. That code worked perfectly and now I can go home for the weekend! :-)
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question