PDA

View Full Version : Solved: Outlook events and VBA



Mcantu
02-14-2008, 02:09 PM
I am VERY new to outlook VBA but have been using some for both Access and Excel.

My task:
To note these fields:
time_Date received / Sent From/ sent to / Subject line/ (What ever else they want)

When an email comes in and when an email goes out.

I have been asked to track productivity as used by outlook. I assume(???) There is a ?received mail? event in outlook and a ?send mail? event in out look that I can just print these fields to a text files somewhere so I can pick it up later and analyze how the person was working.

(They want to make sure they are not skipping emails and working them in date/ time received.)

The printing to text files I am cool with- I just don?t know how to reference these fields, or these events, or if this event handling exists.

I am not quite sure what keyword to search for this, but I am sure someone has done this before.

If you don?t have an answer, a KEYWORD for me to look for the answer may be just as helpful.

THANKS!!!

Oorang
02-18-2008, 12:41 PM
You can find all of the events that Outlook offers by:
1.) Opening the VBE (Alt-F11)
2.) Entering the "ThisSession" module
3.) Change the Left dropdown menu from (General) to Application.
4.) The drop down menu on the right will now list all Outlook.Application events.

Of particular note are: NewMailEx and ItemSend ;)

Charlize
02-19-2008, 06:58 AM
As a starting point you could try this coding (still under development). You need to install the clickyes routine. To get it to work, you'll need to create a rule for every incoming mail to perform this macro and a file Mail_log.xls in a certain diretory (C:\Data\Mail_log) of your disk.
Option Explicit
' Declare Windows' API functions
' To be used with ExpressClick Yes
Private Declare Function RegisterWindowMessage _
Lib "user32" Alias "RegisterWindowMessageA" _
(ByVal lpString As String) As Long
Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" (ByVal lpClassName As Any, _
ByVal lpWindowName As Any) As Long
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long
'These are used by the utility ClickYes Freeware
Public wnd As Long
Public uClickYes As Long
Public Res As Long
Sub Mail_log(myItem As Outlook.MailItem)
Dim i As Long
Dim oExcel As Object
Dim oWb As Object
Dim oWs As Object
Set oExcel = Application.CreateObject("Excel.Application")
oExcel.Visible = False
'C:\Data\Mail_log\Mail_log.xls is the file where we log
'all the mails that come in
Set oWb = oExcel.Workbooks.Open("C:\Data\Mail_log\Mail_log.xls")
Set oWs = oWb.Worksheets(1)
i = 2
Do While oWs.Range("A" & i).Value <> vbNullString
i = i + 1
Loop
Call PrepareClickYes
oWs.Range("A" & i) = myItem.SenderName
Call PerformClickYes
oWs.Range("B" & i).Value = myItem.Subject
oWb.Save
oWb.Close
Set oWs = Nothing
Set oWb = Nothing
Set oExcel = Nothing
End Sub
Sub PrepareClickYes()
'called before attempting to manipulate a message
uClickYes = RegisterWindowMessage("CLICKYES_SUSPEND_RESUME")
wnd = FindWindow("EXCLICKYES_WND", 0&)
Res = SendMessage(wnd, uClickYes, 1, 0)
End Sub
Sub PerformClickYes()
'called directly after the code that manipulates
'a message. Clicks the yes and places the ClickYes utility
'back in suspend mode. When some other routine (that's not
'controlled by you) wants to do something with your
'messages, you still get that warning.
Res = SendMessage(wnd, uClickYes, 0, 0)
End Sub

Mcantu
02-19-2008, 10:31 AM
:thumb
You have given me some great places to start!