INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Connect to specific Outlook Folder

Connect to specific Outlook Folder

(OP)
Below is code I'm working with.
It works just fine. BUT - I need to connect to a different inbox, and a folder in that inbox...
Right now it connects to the folder "HPMS" in MY inbox... But I need to connect to a different inbox - different folder in that inbox..
I can't figure it out...!!

any suggestions..?

Private Sub Form_Load()
'Import Email From The Inbox
Dim rst As Recordset
Dim db As Database
Dim strSearchString As String
Dim strSearchChar As String
Dim strChrPos As String
Dim strBaseMessage As String
'Set up Outlook Objects
Dim Outlook As New Outlook.Application
Dim OutlookNS As Outlook.Namespace
Dim cf As Outlook.MAPIFolder
Dim MyFolder As Outlook.MAPIFolder
Dim MailItem As Outlook.MailItem
Dim objItems As Outlook.Items
Dim iNumContacts As Integer
Dim i As Integer

Set db = CurrentDb()
Set rst = db.OpenRecordset("HPMS_eMails")
Set OutlookNS = Outlook.GetNamespace("MAPI")
Set cf = OutlookNS.GetDefaultFolder(olFolderInbox)
Set MyFolder = cf.Folders("HPMS")
Set objItems = MyFolder.Items

DoCmd.RunSQL "DELETE * FROM HPMS_eMails;"

iNumContacts = objItems.Count
If iNumContacts <> 0 Then
For i = 1 To iNumContacts
If TypeName(objItems(i)) = "MailItem" Then
Set MailItem = objItems(i)

If MailItem.Subject Like "Marketing Event Upload Submission Status*" Then
rst.AddNew
rst!Entry_ID = MailItem.EntryID
rst!Sender = MailItem.Sender
rst!Recieved = MailItem.ReceivedTime
rst!To = MailItem.To
rst!Subject = MailItem.Subject
rst!Body = MailItem.Body
rst!UnRead = MailItem.UnRead
rst.Update
End If
End If
Next i
rst.Close
'DisplayMessage "All Email Has Been Imported"
Else
DisplayMessage "There Is No Email To Be Imported"
End If

Me.lstEmails.Requery

RE: Connect to specific Outlook Folder

Hi,

Can you tell us the folder structure of your Inbox?

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Connect to specific Outlook Folder

(OP)
The mailbox name is:
MarketPOINT Sales Integrity
- Inbox
- Folders
- HPMS Confirmations

So I need to connect to the "HPMS Confirmations" folder in the MarketPOINT Sales Integrity mailbox/inbox...
I guess it would be a sub-subfolder....?
Does that make sense...?

Thank you so much for your help..!!

RE: Connect to specific Outlook Folder

I'm not conversant with the Outlook Object Model. But I have done some coding using the OOM. The tool that was the most helpful, in addition to VBA Help, was/is...

FAQ797-4594: How to use the Watch Window as a Power Programming Tool

The Watch Window is invaluable!

A knowledge of how objects work in general is also very valuable. Collections and structure. So you have folders. Should be a GetFolder() method based on a folder name you supply. Within that folder there may be one or more folders, a folder Collection that you could look through...

CODE

Dim fld As Object

For Each fld In YourFolderObject.Folders

Next 

I can't remember if there were Outlook idiosyncrasies, like Indices, but that's where the WW helps you to identify some of there things.

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Connect to specific Outlook Folder

(OP)
Oh man... This is so close...
I know it just something simple that needs to be changed...
Dang..!

RE: Connect to specific Outlook Folder

air1access,

If something's not working, but you're trying, share a bit more about what's not working, and you'll likely get the help you need.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57

RE: Connect to specific Outlook Folder

(OP)
I am trying to connect to a particular folder in Outlook.
Below gets me to a folder in my main Inbox "folder tree" in Outlook.

What I want to do is connect to a folder in a different mailbox that I use in Outlook.
Its a shared mailbox...

I attached a pic of the folder tree in Outlook to give ya an idea...
Hope this helps - so you can help me..! lol

Thanks in advance..!!

'Set up Outlook Objects
Dim Outlook As New Outlook.Application
Dim OutlookNS As Outlook.Namespace
Dim cf As Outlook.MAPIFolder
Dim MyFolder As Outlook.MAPIFolder
Dim MailItem As Outlook.MailItem
Dim objItems As Outlook.Items


Set OutlookNS = Outlook.GetNamespace("MAPI")
Set cf = OutlookNS.GetDefaultFolder(olFolderInbox)
Set MyFolder = cf.Folders("HPMS")
Set objItems = MyFolder.Items

RE: Connect to specific Outlook Folder

(OP)
The folder "HPMS Confirmations" is the folder I'm needing to connect too...

RE: Connect to specific Outlook Folder

You've got to first get the highest level folder in which your folder of interest resides, NOT OutlookNS.GetDefaultFolder()

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Connect to specific Outlook Folder

(OP)
That's what I'm trying to figure out...

RE: Connect to specific Outlook Folder

???

https://msdn.microsoft.com/en-us/library/office/ff...

Give this a try...

CODE

'
    Set Db = CurrentDb()
    Set rst = Db.OpenRecordset("HPMS_eMails")
    Set OutlookNS = Outlook.GetNamespace("MAPI")
    Set objFolder = OutlookNS.GetDefaultFolder(olFolderInbox).Folders("MarketPOINT Sales Integrity").Folders("HPMS Confirmations") 

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Connect to specific Outlook Folder

air1access,

Look at your code and think about what Skip stated.

OutlookNS.GetDefaultFolder(): What do you think that is: The "Default folder" - that's not the "highest level folder in which your folder of interest resides".

The "Default" folder is configurable in Outlook, but by default is "Inbox". For instance, if what you're looking for happened to be outside of Inbox, then you will never find what you're looking for, b/c it'll be looking within the wrong scope in the folder structure.

https://msdn.microsoft.com/en-us/library/office/ff...

Notice at the link, at the bottom of the explanation, just above the code samples:

Quote:

Remarks
To return a specific non-default folder, use the Folders collection.
If the default folder of the requested type does not exist, depending on the type, Outlook may create and return the folder, or may raise an error. For example, if olFolderManagedEmail is specified as the FolderType but the Managed Folders group has not been deployed, Microsoft Outlook raises an error.

Perhaps that link can give you a little more light at the end of your tunnel.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57

RE: Connect to specific Outlook Folder

(OP)
Oh man... I have been working on this all morning..! lol
Just can't get it to connect to this other mailbox folder.

RE: Connect to specific Outlook Folder

Are you using the Watch Window to inspect appropriate objects?

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Connect to specific Outlook Folder

(OP)
Its gonna take a while to understand how that works.
Kinda pressed for time on this at the moment...

RE: Connect to specific Outlook Folder

Well if you select a Folder object or a Folders object, you'll see all kinds of other objects within that object: stuff you can EXPLOR to possibly DISCOVER what you're looking for.

YOU HAVE TO DO SOME HEAVY LIFTING1

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Connect to specific Outlook Folder

(OP)
Not trying to get you all to do it for me. You gave me plenty of ammo. I'm just having a hard time figuring it out....

RE: Connect to specific Outlook Folder

(OP)
Ok when - I set a Watch on "Dim Outlook As New Outlook.Application" line - on the "Outlook" part...
The watch window tells me the Value <Object variable or With block variable not set>

Man this is over my head... But I'm trying..!

RE: Connect to specific Outlook Folder


Do you have a reference via Tools > References... to the library, Microsoft Outlook m.n Object Library?

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Connect to specific Outlook Folder

(OP)
yes. Sure do.
Microsoft Outlook 14.0 Object Library

RE: Connect to specific Outlook Folder

A declaration is not an executable statement.

STEP through your code until you arrive at a statement containing that object.

Also, do you have Option Explicit as the first statement in your module?

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Connect to specific Outlook Folder

(OP)
The struggle is real..!!

I do have Option Explicit... I tried with & without it...

'Set up Outlook Objects
Dim OutlookApp As New Outlook.Application
Dim OutlookNS As Outlook.Namespace
Dim cf As Outlook.MAPIFolder
Dim MyFolder As Outlook.MAPIFolder
Dim MailItem As Outlook.MailItem
Dim objItems As Outlook.Items
Dim iNumContacts As Integer
Dim i As Integer

Set OutlookNS = OutlookApp.GetNamespace("MAPI")
Set cf = OutlookNS.GetDefaultFolder(olFolderInbox)
Set MyFolder = cf.Folders("MarketPOINT Sales Integrity").Folders("Inbox").Folders("HPMS Confirmations")
Set objItems = MyFolder.Items

I can't get past -- Set MyFolder = cf.Folders("MarketPOINT Sales Integrity").Folders("Inbox").Folders("HPMS Confirmations") -- It generates an error here.
Says: "Object cannot be found"

RE: Connect to specific Outlook Folder

Quote:

I do have Option Explicit... I tried with & without it.

You should NEVER EVER try to execute code without Option Explicit. This has NOTHING to do with any specific line of code. It is your safety net, that your code contains no "illegal" stuff.

With the assumption that you have Option Explicit...

...what do you see in cf after you execute the Set cf = ... statement?

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Connect to specific Outlook Folder

(OP)
SkipVought,
I'm having a hard time following ya...
I really don't know how to get this to work they way I want. I know its possible - I just don't understand how to connect to the different mailbox, and the folders in that mailbox....
I don't understand how to use the watch tool, or understand the error messages....

RE: Connect to specific Outlook Folder

air1access,

It's easy for any of us to do: keep worrying about the end goal instead of the tiny steps to reach the goal. The latest items Skips trying to help you with are trying to get those small steps covered. When you're dealing with code, it's often the tiny minuscule items that can cause something "not to work". Just take a breathe - or by now I guess you have smile, and try to work through the questions and answers. You could end up solving the whole problem once you get through one or two small quirks/issues.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57

RE: Connect to specific Outlook Folder

"I don't understand how to use the watch tool."

Did you carefully read the FAQ I posted? It's detailed. Don't give up. There are some things code wise, that only YOU can do.

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close