outlook folders

CHAOSinACT

Registered User.
Local time
Today, 20:53
Joined
Mar 18, 2009
Messages
235
OK, trying to import and attach emails from outlook to my access dBase.

have working code to do it, but need to give the user a way to only link emails related to a specific project.

I've been toying with the idea of using subfolders, ie emails related to job 096 live in the 096 subfolder in outlook. this will work but it would be NICE (lol!) if i could read all subfolders in outlook, present them in an access listbox and let the user pick which subfolders to add on the fly. having trouble finding code examples that do that, anyone got any or can point me in right direction? thanx in advance :)
 
I've been toying with the idea of using subfolders, ie emails related to job 096 live in the 096 subfolder in outlook. this will work but it would be NICE (lol!) if i could read all subfolders in outlook, present them in an access listbox and let the user pick which subfolders to add on the fly. having trouble finding code examples that do that, anyone got any or can point me in right direction? thanx in advance :)

You can get all subfolders of a default folder in outlook. But who or what will add these sub folders. Do you mean that the user will add his own sub folders and then when you display a form in access you want your drop down box to show them all. Or do you want access to make them?

You cannot protect a folder from being deleted so what happens in access if the user deletes one in outlook?
 
You can get all subfolders of a default folder in outlook. But who or what will add these sub folders. Do you mean that the user will add his own sub folders and then when you display a form in access you want your drop down box to show them all. Or do you want access to make them?

You cannot protect a folder from being deleted so what happens in access if the user deletes one in outlook?

i'm thinking more that you jump into a form for attaching mail, it loads whatever subfolders are their for user selection at the time. import only, not pushing into outlook at all. frankly only have 10 users max here, 3 that might use this facility (for now fast growing company) and i'm happy to have some user training. we were going to just have them strictly create folders with a naming convention and that would do it, just trying to be fancy.

i'm new to outlook programming and just not that familiar with the event model. any good resources on this? i haven't found what i'm looking for - i can reference a folder directly (and i may stumble on it lol) but would love a good reference...
 
Last edited:
hahahahahaha!!!! did it all on my own!!! yay me. i wing my entire life from online forums etc, and OFTEN people work out final result and don't post it cause they are happy. for posterity and the help of others:

here is the code to read the subfolders in the outlook inbox. u need to have a table called "tblOutlookFolders" with an autonumber ID FIELD and a field called "FolderName":


Public Sub GetInboxSubfolders()
Dim TempRst As DAO.Recordset
Dim olApp As Outlook.Application
Dim Inbox As Outlook.MAPIFolder
Dim Folder As Folder
'clear folder list, open rs
DoCmd.RunSQL "Delete * from tblOutlookFolders"
Set TempRst = CurrentDb.OpenRecordset("tblOutlookFolders")

Set olApp = CreateObject("Outlook.Application")
Set Inbox = olApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox) 'set inbox to inbox...

For Each Folder In Inbox.Folders

With TempRst

.AddNew
Debug.Print Folder
!FolderName = Folder
.Update

End With

Next
Set olApp = Nothing
Set Inbox = Nothing
Set Mailobject = Nothing
Set TempRst = Nothing
End Sub


here is code that will import all the emails from a specific outlook folder and import into a table called "tbl_OutlookTemp" (thanks to blue claw for the base code for this, it has been modified). the table must have Subject(text), From(text), To(text), Body(as memo, use rich text and hyperlinks work :)), DateSent (as date/time):

Public Sub ReadInbox(strSentFolder As String)
Dim TempRst 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 SubFolder As Folder

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")
Set SubFolder = Inbox.Folders(strSentFolder)
Set InboxItems = SubFolder.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

End Sub

call it like this:

Public Sub TestEmailFolder()
Dim strFolder As String
strFolder = "096"
Call ReadInbox(strFolder)
End Sub




also, replace the docmd.runSQL "delete..." statement with this:

If Not TempRst.EOF Then
Do Until TempRst.EOF
With TempRst
.Delete
.MoveNext
End With
Loop
TempRst.MoveFirst
End If

it stops it prompting the user before deleting the table.
 
Last edited:
Good on you - doing is learning - hope if feels good and your users like it.

1 things I saw

Code:
Set InboxItems = SubFolder.Items

For Each Mailobject In InboxItems
    'If Mailobject.UnRead Then
    With TempRst
An Inbox can have more than emails in it. Currently you are not checking that the itemtype is a mailitem. you might like to do that.
 
ok i admit blatant ignorance....WTH else would be in there???
 
Here are somethings you might want to do with your Mailobject

For Each Mailobject In InboxItems

Code:
'check that it is in fact an email that has been choose
If Not Mailobject.Class = olMail Then
    'do something as it is not an email
End If


'next try to get the email item - if there is an error 13 then it is encrypted
On Error Resume Next

Set out_mail = Mailobject

If Err.Number = 13 Then
    Err.Clear
    'do something as you cannot do anything with the email 
End If
One other thing I noticed

you use this
Code:
!UniqueID = Mailobject.EntryID
The EntryID of all items in Outlook changes. What a stupid ID. If you move an item to another folder (which might be happening for you) then its ID changes.

If you want to find your EmailItem again from Access I would suggest you do the following.

In your table have an Autonumber ID - with a new email add a new record to your table. Once it is added get your autonumber for the record. Then add this number to the .mileage property of the EmailItem. Then move onto the next emailitem.

Then if you ever want to find the email again (no matter where it is in outlook) you can do it with access by searching for this number. A user can find and remove this property but they would have to really go searching for it.
 
thanks thats very helpful. the whole point of the entryID was to have a Unique identifier that works in outlook and access. ie. the user adds 2 emails to the directory that they added to from before. i need to know that we don't re-import the same email again and again. so SURELY (LOL!!!) outlook has somekind of unique id within itself????? please say yes.... if not i shall diligently follow your last advice. i would have though outlook would need one to function really
 
outlook has somekind of unique id within itself????? please say yes.... if not i shall diligently follow your last advice. i would have though outlook would need one to function really
I have found myself saying "surely MS did..........."
The answer is and I am pretty sure I am right, the ID is unique but can change. I do not know all of the reasons it changes but i do know that if you move an item to another folder the ID will change.

Thus I would suggest you stick with my suggestion. It is not that much more coding. Then to check if you have already added an item or not just check if anything is in this .mileage property. I have never seen someone use it yet.
 
I have found myself saying "surely MS did..........."
The answer is and I am pretty sure I am right, the ID is unique but can change. I do not know all of the reasons it changes but i do know that if you move an item to another folder the ID will change.

Thus I would suggest you stick with my suggestion. It is not that much more coding. Then to check if you have already added an item or not just check if anything is in this .mileage property. I have never seen someone use it yet.

fair enough!!! seems crazy but i hear ya - assumptions and microsoft go together like whisky and guns...

cheers!
 

Users who are viewing this thread

Back
Top Bottom