itemchange event in public (exchange) folder occurs if nothing is changed

M

MIchael

Hello
I am monitoring a public contacts folder (on an exchange server). Every time a contact is changed a program should be launched.
But the event occures even when the contact is only opened. Nothing needs to be changed to let the event fire.
Do I need to take an other event?
My code is below
Thank you very much for your help!!!!
Michael

Public WithEvents colPubCustomersItems As Outlook.Items

Public Sub Application_Startup()
Set objFolders = Session.GetDefaultFolder(olFolderInbox).Parent.Folders
Dim strFolderPath As String
strFolderPath = "Öffentliche Ordner\Alle Öffentlichen Ordner\Kontakte von AS-DA2"
Set myolapp = CreateObject("Outlook.Application")
Set myNamespace = myolapp.GetNamespace("MAPI")
Set colPubCustomersItems = myNamespace.Folders("Öffentliche Ordner").Folders("Alle Öffentlichen Ordner").Folders("Kontakte von
AS-DA2").Items
End Sub

Private Sub colPubCustomersItems_Itemchange(ByVal Item As Object)
Dim objCont As Outlook.ContactItem
Set objCont = Item
Shell "C:\Programme\prog_to run.exe", 0 'vbnormal
End Sub
 
K

Ken Slovak - [MVP - Outlook]

That's not normal, I can open items in public folders without firing
Item_Change.

Do the items in that folder run any code that changes something when the
item is opened? Is there any macro code or other code that would run on open
that would change the item?
 
M

MIchael

Thank you very much for your answer Ken!
No, there is (nearly :) no other code or anything that can make it fire. I will post everything below.
Is there any possibility to trap what makes the thing run?
ciao
Michael

Dim WithEvents objFolders As Outlook.Folders
Public WithEvents colPubCustomersItems As Outlook.Items
Public WithEvents colJournalItems As Outlook.Items
'Instantiate collection object in objOutlook Startup event
'um eays2sync anzuwerfen bei Änderung im öff Ordner

Public Sub Application_Startup()
Set objFolders = Session.GetDefaultFolder(olFolderInbox).Parent.Folders
Dim strFolderPath As String
strFolderPath = "Öffentliche Ordner\Alle Öffentlichen Ordner\Kontakte von AS-DA2"
Set myolapp = CreateObject("Outlook.Application")
Set myNamespace = myolapp.GetNamespace("MAPI")
Set colPubCustomersItems = myNamespace.Folders("Öffentliche Ordner").Folders("Alle Öffentlichen Ordner").Folders("Kontakte von
AS-DA2").Items
Set colCustomersItems = myNamespace.GetDefaultFolder(olFolderContacts).Items
Set colJournalItems = myNamespace.GetDefaultFolder(olFolderJournal).Items
'Alles sync beim Outlook start
Shell "C:\Programme\Easy2Sync für Outlook\E2S4Outlook.exe /syncallandexit /nosplash /Delayed:10", 0
End Sub

Private Sub colPubCustomersItems_Itemchange(ByVal Item As Object)
Dim objCont As Outlook.ContactItem
Set objCont = Item
Shell "C:\Programme\Easy2Sync für Outlook\E2S4Outlook.exe /syncandexit:kontakte_dirksen /nosplash", 0 'vbnormal
End Sub

Private Sub colJournalItems_Itemchange(ByVal Item As Object)
Dim objJour As Outlook.JournalItem
Set objJour = Item
Shell "C:\Programme\Easy2Sync für Outlook\E2S4Outlook.exe /syncandexit:journal_dirksen /nosplash", 0
End Sub

Sub objFolders_FolderChange(ByVal Folder As Outlook.MAPIFolder)
On Error GoTo ErrorHandler ' Fehlerbehandlung aktivieren.
If Folder.Name = "Kontakte" Then
Dim objContactsFolder As Outlook.MAPIFolder
Dim objContacts As Outlook.Items
Dim objContact As Object
Dim iCount As Integer

' Zu verwendenden Kontaktordner angeben
Set objContactsFolder = Session.GetDefaultFolder(olFolderContacts)
Set objContacts = objContactsFolder.Items

iCount = 0

' Änderungen verarbeiten
For Each objContact In objContacts
If TypeName(objContact) = "ContactItem" Then
If objContact.Journal = False Then
objContact.Journal = True
objContact.Save
iCount = iCount + 1
End If
End If
Next

' MsgBox "Anzahl aktualisierter Kontakte:" & Str$(iCount)

' Aufräumen
ErrorHandler:
Set objContact = Nothing
Set objContacts = Nothing
Set objContactsFolder = Nothing
Exit Sub
End If
End Sub
 
K

Ken Slovak - [MVP - Outlook]

This is in an Outlook addin? Never instantiate an Outlook Application object
in an Outlook addin. Use the inherited Application object passed to you.

It looks to me like that program you're shelling out to does some sort of
synching. If that's so that's where your change events are probably coming
from.

To debug it I'd probably set handlers on the items being changed to handle
the PropertyChange event. That would at least tell you what was being
changed in the item and might provide a clue. For an event handler like that
a review of the call stack is probably useless.
 
M

MIchael

Hello Ken,
Thats not an addin, its only code in thisoutlooksession (DieseOutlookSitzung in German:)
I changed the program thats shelled for testing to Shell "c:\windows\system32\calc.exe"
the windows calculator. And its just the same .
If I open an contact the event is firing ;-((
But the funny thing is that not every time when I open the contact it is firing it seems to do it randomly.
the first 5 times opening any contact- not firing
2 times opening - firing
2 more times opening an other contact not firing...................

Sorry but I cant do anything with your proposal
"To debug it I'd probably set handlers on the items being changed to handle the PropertyChange event."
I am beginner :-((
Maybe you could give me an other hint what do you mean by "to set handlers"??
Thank you very much for your help!!
Michael
 
K

Ken Slovak - [MVP - Outlook]

Code in ThisOutlookSession also should use the intrinsic Application object
and not create an Outlook.Application object.

Something you are running, whether it's the sync code or something else is
changing the items or Item.Change wouldn't fire.

If you declare an item object WithEvents you can handle the events for that
item. So for a MailItem:

Dim WithEvents oMail as Outlook.MailItem

Then in the NewInspector event handler:
If Inspector.Class = olMail Then
Set oMail = Inspector.CurrentItem
End If

Select oMail in the drop-down at the top left of the code window and in the
right-hand drop-down select the PropertyChange event. You can also select
CustomPropertyChange. Write code for those event handlers to see which
properties are being changed. The property name is passed to you as Name in
those events.
 
M

MIchael

Hello Ken
now I have an idea whats changed in the folder and makes the event fire:
1. the change event is only firing (if nothing is changed) with contacts in an public! folder on the exchange server
2. it is not firing (if nothing is changed) in an personal folder (also on an exchange server)
3. if one changes the properties settings of the contacts folder on the exchange server from "keep read/unread information per
user for this folder" to not to keep ....(remove checkmark)
the event fires only the first time one contact is opened.
So it seems that a kind of read/unread attribute is toggled and that makes the event fire

Do you have maybe any further idea to prevent the first firing?
Thanks
Michael
 
K

Ken Slovak - [MVP - Outlook]

If it's a server side setting about the only thing you can do other than
mandating that setting is to work around the problem. Handle the test events
and use which ones fire to construct a logic tree to decide when to handle
Change on Open or Read or PropertyChange or whatever and when to ignore that
event.
 

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