Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connect to specific Outlook Folder 1

Status
Not open for further replies.

air1access

Technical User
Jan 27, 2008
123
US
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
 
Hi,

Can you tell us the folder structure of your Inbox?

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
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..!!
 
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

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,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
Oh man... This is so close...
I know it just something simple that needs to be changed...
Dang..!
 
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
 
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

 
 http://files.engineering.com/getfile.aspx?folder=611fd3b9-e2d6-448b-af41-81b536d1a883&file=MailboxFolderTree.JPG
The folder "HPMS Confirmations" is the folder I'm needing to connect too...
 
You've got to first get the highest level folder in which your folder of interest resides, NOT OutlookNS.GetDefaultFolder()

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
???


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,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
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.


Notice at the link, at the bottom of the explanation, just above the code samples:
Remarks
To return a specific [highlight #FCE94F]non-default folder[/highlight], 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
 
Oh man... I have been working on this all morning..! lol
Just can't get it to connect to this other mailbox folder.
 
Are you using the Watch Window to inspect appropriate objects?

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
Its gonna take a while to understand how that works.
Kinda pressed for time on this at the moment...
 
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,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
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....
 
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..!
 

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

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top