Record eMail details in Outlook 2007

  • Thread starter Thread starter scrawny
  • Start date Start date
S

scrawny

Does anyone know how to grab certain information out of an email item
(such as subject, date of receipt, sender etc.).
I can capture this information on Application.NewMail... but to throw
a spanner in the works - I also want the folder name recorded as well
where the user places it.

For example, if a user has Folder 1, Folder 2, Folder 3 under their
Inbox... And as soon as a mail arrives to their inbox they read it and
then decide it needs to go to Folder 2... can I capture the emails
information with the chosen folder at this point?

Cheers,

Scrawny.
 
You can handle the ItemAdd() event on the Items collection of any folders
you want. Get the folder, get its Items collection, then subscribe to the
ItemAdd() event.
 
Thanks for your reply... sorry if I'm a bit slow to catch on, but
where would I be going wrong in the following code:

Dim oApp As New Outlook.Application
Public WithEvents oFolder1 As Outlook.Folder
Public WithEvents oFolder2 As Outlook.Folder
Public WithEvents oFolder3 As Outlook.Folder

Private Sub Application_Startup()
Set oFolder1 = oApp.Session.GetDefaultFolder
(olFolderInbox).Folders.Item("Folder 1")
Set oFolder2 = oApp.Session.GetDefaultFolder
(olFolderInbox).Folders.Item("Folder 2")
Set oFolder3 = oApp.Session.GetDefaultFolder
(olFolderInbox).Folders.Item("Folder 3")
End Sub

Private Sub oFolder1_ItemAdd(ByVal Item As Object)
Dim oMail As Outlook.MailItem

Set oMail = Item
MsgBox oMail.Subject
End Sub

.... Other Folder objects have the same portion of code as that last
ItemAdd sub.

At the moment I'm just returning the subject to a message box just for
test purposes.

Cheers,

Scrawny
 
What do you mean where are you going wrong, what's not working?

I see 2 things offhand, in Outlook VBA code you never, ever create a new
Outlook.Application object, you use the Application object that's handed to
you and which is trusted. Second, don't assume that what comes to a folder
is necessarily a mail item. It could be a post item, or in Inbox it could be
a task request, meeting request, etc. Use Item.Class to test for olMail to
see if it's really a mail item.
 
No worries... that makes sense.

But how do I declare the folders which reside under the inbox?
Is that Set command in Application_Startup accurate or is there a
better way.
By the way, I am safely assuming that these folders have already been
set up.

The actual code above does allow the declarations of oFolder1, 2, 3...
but on nothing happens when I add a mail item to the folder. So it's
obviously not declaring the right folder. Or the variable isn't
pointing to the folder that I think it should be.

-Scrawny
 
If the folders don't exist then your return values for Folder1, etc. would
be null (Nothing). So you can test and create the folder if it doesn't
already exist:

Set oFolder1 =
oApp.Session.GetDefaultFolder(olFolderInbox).Folders.Item("Folder 1")
If oFolder1 Is Nothing Then
Set oFolder1 =
oApp.Session.GetDefaultFolder(olFolderInbox).Folders.Add("Folder 1",
olFolderInbox) 'watch for line wrap on this, it's all one line
End If

Step your code and make sure that you get valid Folder objects as the code
runs. You can also check the Items.Count property for each of those folders
to check the number of items in the folder.

Assuming the folders are valid the ItemAdd() event handlers look OK.
 
Back
Top