Begbie76
asked on
Downloading Emails from mailbox other than default to MS Access using VBA
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") .GetDefaul tFolder(ol FolderInbo x)
Thanks in advance.
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")
Thanks in advance.
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.
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.
ASKER
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") .GetDefaul tFolder(ol FolderInbo x)
The group mailbox is called 'OCC-CardAdmin'.
Set Inbox = OlApp.GetNamespace("Mapi")
The group mailbox is called 'OCC-CardAdmin'.
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.CreateRecipien t("<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.GetSharedDefau ltFolder _
(myRecipient, olFolderCalendar)
InboxFolder.Display
End Sub
Sub ResolveName()
Dim myNamespace As Outlook.NameSpace
Dim myRecipient As Outlook.Recipient
Dim CalendarFolder As Outlook.Folder
Set myNamespace = Application.GetNamespace("
Set myRecipient = myNamespace.CreateRecipien
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.GetSharedDefau
(myRecipient, olFolderCalendar)
InboxFolder.Display
End Sub
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.CreateRecipien t("<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.GetSharedDefau ltFolder _
(myRecipient, olFolderInbox)
InboxFolder.Display
End Sub
Sub ResolveName()
Dim myNamespace As Outlook.NameSpace
Dim myRecipient As Outlook.Recipient
Dim CalendarFolder As Outlook.Folder
Set myNamespace = Application.GetNamespace("
Set myRecipient = myNamespace.CreateRecipien
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.GetSharedDefau
(myRecipient, olFolderInbox)
InboxFolder.Display
End Sub
ASKER
Just tried that. I'm getting a 'Method or data member not found' error at .GetNamespace.
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.CreateRecipien t("'OCC-Ca rdAdmin'")
myRecipient.Resolve
If myRecipient.Resolved Then
Set Inbox = myNamespace.GetSharedDefau ltFolder(m yRecipient , olFolderInbox)
Inbox.Display
End If
End Sub
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.CreateRecipien
myRecipient.Resolve
If myRecipient.Resolved Then
Set Inbox = myNamespace.GetSharedDefau
Inbox.Display
End If
End Sub
ASKER
Just tried that. On the If statement myRecipient isn't resolved, so it's just jumping to End If.
Is it MS Outlook which is your e-mail application?
What is displayed for the other mailbox in your MS Outlook, similar to the print screen I have attached?
MailBoxPrintScreen.jpg
MailBoxPrintScreen.jpg
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
Doc1.doc
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
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
ASKER
Thank you so much. That code worked perfectly and now I can go home for the weekend! :-)
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