NEWBIE - - - - - outlook gets in endless loop!

V

vonclausowitz

Hi all,
We share a mailbox and tasks with four persons. I use this code to
catch a selection in my tasks and if a task is changed the user defined
field "Updater" is changed. There are two problems with this code:

1. first of all, if someone else then me changes a task the code will
think it is me and update the field using my username;
2. secondly if I change a task and this task is completed, the field
Updater gets changed accordingly, the task is marked as Completed but
then Outlook gets like crazy and is unaccessable.

I don't know if if has something to do with the fact that the tasks
have a daily recurrence?


Option Explicit

Dim gUser As String
Public sValueRole As String
Public sValueDueDate As String
Public sValueSubject As String
Public strCurrentUser As String
Public nmsUser As Outlook.NameSpace
Public newTaskFolder As MAPIFolder

Public WithEvents oTask As Outlook.TaskItem 'definieer een taak
Public WithEvents oTasks As Outlook.Items 'definieer de taken
Public WithEvents oExplorer As Outlook.Explorer
Public WithEvents oApplication As Outlook.Application

Private Sub Application_Startup()

gUser = vbGetUserName()
Initialize_handler

End Sub

Public Sub Initialize_handler()

Dim oNS As Outlook.NameSpace
Set oNS = Application.GetNamespace("MAPI")

If gUser = "Marco" Then
Set newTaskFolder =
oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks).Folders("Taken
Oud")
Set oTasks =
oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks).Items
End If

Set oExplorer = Application.ActiveExplorer
Set oApplication = Outlook.Application

strCurrentUser = gUser

End Sub

Private Sub oExplorer_SelectionChange()

If oExplorer.CurrentFolder.Name = "Taken" Then
If strCurrentUser = "Marco" Then
If oExplorer.Selection.Count <> 0 Then
Set oTask = oExplorer.Selection(1)
sValueRole = oTask.Role
sValueDueDate = oTask.DueDate
sValueSubject = oTask.Subject
End If
End If

End If

End Sub

Private Sub oTasks_ItemChange(ByVal item As Object)

Dim Response As Integer
Dim oTask As TaskItem

Set oTask = item

If Not oTask.Role = "" And Not sValueRole = "" Then
If CDate(oTask.Role) > CDate(sValueRole) Then
oTask.UserProperties("Updater") = strCurrentUser & " " &
Format(Date, "DD-MM-YYYY")
oTask.Save
If (CDate(oTask.Role) = Date - 1 And oTask.DueDate = Date) Or
(CDate(oTask.Role) = Date And oTask.DueDate = Date) Then
'wil je de taak afvinken?
Response = MsgBox("Wil je deze taak afvinken '" &
oTask.Subject & "'?", vbYesNo, "Doorgaan")
If Response = 6 Then
oTask.Status = olTaskComplete
oTask.Save
End If
End If
End If
End If

End Sub

What I'm asking is:

1. when using these tasks with four users on different machines how can
the code tell who is updating the task?
2. why does Outlook get in a loop when I update and complete one task?

Regards
Marco
The Netherlands
 
K

Ken Slovak - [MVP - Outlook]

Every time you change the item in ItemChange you then force re-entry to that
event since you are changing the item yourself within the event handler.

There's no Outlook object model property for who changed a task item last,
and if the code is running on your computer and your code changes a property
in the item then you would be the last user to change the property anyway.

There is a MAPI property for who changed a task item last:
PR_LAST_MODIFIER_NAME (0x3FFA001E), a PT_STRING8 property. Even though it's
not available in the Outlook object model it is available to CDO 1.21 or
Extended MAPI (C++ or Delphi only) or to Redemption (3rd party library at
www.dimastr.com/redemption). Since CDO is an optional installation for
Outlook 2000 and later and I use Redemption extensively for things I can't
do with the OOM and for avoiding the security of the object model guard, I
would use Redemption to get at PR_LAST_MODIFIER_NAME if this were my code.

The way I would handle this is to use Selection.Change and the first
Activate event for an Explorer to get the selected item if only one was
selected. I would also track the opening of Inspectors to handle any changes
to items that were opened rather than being edited using in-cell editing in
a folder view.

Once I had an Item instantiated from those 2 environments I would then
handle the Item.PropertyChange and Item.CustomPropertyChange events. In
those events I would check for which property was changed and if it was one
I was interested in I would then run my code to modify the item the way you
want.
 
V

vonclausowitz

Wow,

sounds complicated. What if I installed Redemption?
Could you give me an example of how my code should look like?

Can I use the free version of Redemption without limitations?

Marco
 
K

Ken Slovak - [MVP - Outlook]

Please post parts of the preceding thread in your posts, it makes it hard to
follow a conversation otherwise.

The free, personal version of Redemption is not for distribution. So that
version could only be used on your own computer and cannot be distributed to
other people. If you need to do that you will have to purchase a license for
the distributable version of Redemption.

Redemption would allow you access to all the MAPI properties you would need
for what you want to do, in conjunction with the Outlook object model.

I would suggest that you try creating the code yourself and then posting
samples of it if you run into problems.
 
V

vonclausowitz

Would this code work in a multi user environment?
Will it track the user who changes an Item or will it always return my
username even I if didn't change anything, just because the code is on
my machine?

Option Explicit

Dim gUser As String
Public sValueRole As String
Public sValueDueDate As String
Public sValueSubject As String
Public strCurrentUser As String
Public nmsUser As Outlook.NameSpace
Public newTaskFolder As MAPIFolder

Public WithEvents oTask As Outlook.TaskItem 'definieer een taak
Public WithEvents oTasks As Outlook.Items 'definieer de taken
Public WithEvents oExplorer As Outlook.Explorer
Public WithEvents oApplication As Outlook.Application

Private Sub Application_Startup()

gUser = vbGetUserName()
Initialize_handler

End Sub

Public Sub Initialize_handler()

Dim oNS As Outlook.NameSpace
Set oNS = Application.GetNamespace("MAPI")

If gUser = "Marco" Then
Set newTaskFolder =
oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks).Folders("Taken
Oud")
Set oTasks =
oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks).Items
End If

Set oExplorer = Application.ActiveExplorer
Set oApplication = Outlook.Application

strCurrentUser = gUser

End Sub

Private Sub oExplorer_SelectionChange()

If oExplorer.CurrentFolder.Name = "Taken" Then
If strCurrentUser = "Marco" Then
If oExplorer.Selection.Count <> 0 Then
Set oTask = oExplorer.Selection(1)
sValueRole = oTask.Role
sValueDueDate = oTask.DueDate
sValueSubject = oTask.Subject
End If
End If

End If

End Sub

Private Sub oTasks_ItemChange(ByVal item As Object)

Dim Response As Integer
Dim oTask As TaskItem

Set oTask = item

If Not oTask.Role = "" And Not sValueRole = "" Then 'de waarden mogen
niet leeg zijn
'als de waarde in het roleveld nieuwer is dan...
If CDate(oTask.Role) > CDate(sValueRole) Then
oTask.UserProperties("Updater") = strCurrentUser & " " &
Format(Date, "DD-MM-YYYY")
oTask.Save
If (CDate(oTask.Role) = Date - 1 And oTask.DueDate = Date) Or
(CDate(oTask.Role) = Date And oTask.DueDate = Date) Then
Response = MsgBox("Wil je deze taak afvinken '" &
oTask.Subject & "'?", vbYesNo, "Doorgaan")
If Response = 6 Then
oTask.Status = olTaskComplete
oTask.Save
End If
End If
End If
End If

End Sub

Marco
 
K

Ken Slovak - [MVP - Outlook]

Any COM addin runs in-process with Outlook and if more than one user is
running the same code on the same folder then you can have various
conflicts. It begins to sound more and more that what you really need to do
is to write an Exchange folder event sink, which wouldn't use the Outlook
object model at all.
 
V

vonclausowitz

Is there anything on writing such an Exchange folder event sink around?

Marco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top