Read-only code for public folder items to prevent conflicts

A

Andi

Thanks to some earlier posts, I'm trying some new code. I'll be taking
out some of the message boxes (they probably seem silly to you), but I
wanted to see how things were working as it stepped through the code.
The code in the item_close event seems backwards to me, but I still
need to test it out with another user to see what happens when we both
have the record open. What I'm asking for is that if any gurus could
review the code below and tell me if there is anything else I should do
to make this code complete, or if there is something that needs to be
fixed, etc.

Any feedback from the pros is greatly appreciated.
Thanks so much!
Andrea

'module level declaration:
Dim blnReadOnly
Function Item_Open()
If Item.UserProperties("ReadOnly") Then
blnReadOnly = True
msgbox "This item IS ALREADY OPEN by another user. Your changes WILL
NOT be saved"
Else
blnReadOnly = False
item.userproperties.find("ItemOpenUser").value =
application.getnamespace("MAPI").currentuser
msgbox ("Congratulations " & item.userproperties("ItemOpenUser") & "!
This item is NOT currently open by another user. Your changes WILL be
saved")
End If
End Function
'check in Item_Write and if True then Item_Write = False
'in Item_Close if Item.UserProperties("ReadOnly") then set it False and
set blnReadOnly False and save.

Function Item_Write()
if blnReadOnly=true then
item_write=false
msgbox ("Remember " & application.getnamespace("MAPI").currentuser &
" This item IS ALREADY OPEN by the user - " &
item.userproperties("ItemOpenUser") & ". Your changes WILL NOT be
saved")
else
item_write=true
msgbox ("Hooray " & application.getnamespace("MAPI").currentuser & "!
This item is NOT currently open by another user. Your changes WILL be
saved")
end if
End Function


Function Item_Close()
If Item.UserProperties("ReadOnly") Then

msgbox ("Did your changes get saved " &
application.getnamespace("MAPI").currentuser & " ? I think not!")
blnReadOnly = false
item.save
else
msgbox ("As you save and close this item " &
application.getnamespace("MAPI").currentuser & ", you'll be happy to
know that your changes WILL be saved")
blnReadOnly=true
End If

End Function
 
S

Sue Mosher [MVP-Outlook]

Yes, the Close event is backwards. Comments inline.

FYI, there is a newsgroup specifically for Outlook forms issues "down the hall" at microsoft.public.outlook.program_forms or, via web interface, at http://www.microsoft.com/office/com...spx?dg=microsoft.public.outlook.program_forms

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



Andi said:
Thanks to some earlier posts, I'm trying some new code. I'll be taking
out some of the message boxes (they probably seem silly to you), but I
wanted to see how things were working as it stepped through the code.
The code in the item_close event seems backwards to me, but I still
need to test it out with another user to see what happens when we both
have the record open. What I'm asking for is that if any gurus could
review the code below and tell me if there is anything else I should do
to make this code complete, or if there is something that needs to be
fixed, etc.

Any feedback from the pros is greatly appreciated.
Thanks so much!
Andrea

'module level declaration:
Dim blnReadOnly
Function Item_Open()
If Item.UserProperties("ReadOnly") Then
blnReadOnly = True
msgbox "This item IS ALREADY OPEN by another user. Your changes WILL
NOT be saved"
Else
blnReadOnly = False
item.userproperties.find("ItemOpenUser").value =
application.getnamespace("MAPI").currentuser

' here you need to add:

Item.UserProperties("ReadOnly") = True
Item.Save

' so that the next user will see the property values
' that indicate the item is already open
msgbox ("Congratulations " & item.userproperties("ItemOpenUser") & "!
This item is NOT currently open by another user. Your changes WILL be
saved")
End If
End Function





'check in Item_Write and if True then Item_Write = False
'in Item_Close if Item.UserProperties("ReadOnly") then set it False and
set blnReadOnly False and save.

Function Item_Write()
if blnReadOnly=true then
item_write=false
msgbox ("Remember " & application.getnamespace("MAPI").currentuser &
" This item IS ALREADY OPEN by the user - " &
item.userproperties("ItemOpenUser") & ". Your changes WILL NOT be
saved")
else
item_write=true

' You don't really need the above line
msgbox ("Hooray " & application.getnamespace("MAPI").currentuser & "!
This item is NOT currently open by another user. Your changes WILL be
saved")
end if
End Function


Function Item_Close()
If Item.UserProperties("ReadOnly") Then

msgbox ("Did your changes get saved " &
application.getnamespace("MAPI").currentuser & " ? I think not!")
blnReadOnly = false

You don't need the above statement since the item is closing. And you don't want the statement below.
item.save
else
msgbox ("As you save and close this item " &
application.getnamespace("MAPI").currentuser & ", you'll be happy to
know that your changes WILL be saved")

' save the item one more time, resetting the properties that
' control whether it's handled as read-only

item.userproperties.find("ItemOpenUser") = ""
Item.UserProperties("ReadOnly") = False
Item.Save
blnReadOnly=true

I think you'll need to remove the above line in order to be able to handle the case where the user closes the item, and then says Yes to the chooses Save Changes? prompt.
 
A

Andi

Thank you Sue for your speedy reply! Here's the changes with the
remarked out code, however the items continue to say they are open even
though they are not, making it impossible for other users to be able to
make changes to the item. With the 2 test items closed the custom view
is diplaying the ItemOpenUser field and the ReadOnly field. The
ItemOpenUser field continues to keep my name in it, and the ReadOnly
field says Yes. This is the only code on this form. What do you think
might be happening that somehow keeps the items open even after they
have been closed? Do you think the Exchange Server is affecting the
setting? The only reason I ask is that we have been having a lot of
public folder conflict messages over the last 2 month and people SWEAR
they are closing the items. I did some troubleshooting and couldn't
really find a cause, so I thought I would agressively look for some
code I could put on the form to create a read-only state. That's what
brings me here today. It makes me wonder...

oh, and yes...I just found the website for the other newsgroup and
already have it marked as a favorite. Thanks! Should I post it there
as well?

'module level declaration:
Dim blnReadOnly
Function Item_Open()
If Item.UserProperties("ReadOnly") Then
blnReadOnly = True
msgbox "This item IS ALREADY OPEN by another user. Your changes WILL
NOT be saved"
Else
blnReadOnly = False
item.userproperties.find("ItemOpenUser").value =
application.getnamespace("MAPI").currentuser
' so that the next user will see the property values
' that indicate the item is already open
Item.UserProperties("ReadOnly") = True
Item.Save

msgbox ("Congratulations " & item.userproperties("ItemOpenUser") & "!
This item is NOT currently open by another user. Your changes WILL be
saved")
End If
End Function

'check in Item_Write and if True then Item_Write = False
'in Item_Close if Item.UserProperties("ReadOnly") then set it False and
set blnReadOnly False and save.

Function Item_Write()
if blnReadOnly=true then
item_write=false
msgbox ("Remember " & application.getnamespace("MAPI").currentuser &
" This item IS ALREADY OPEN by the user - " &
item.userproperties("ItemOpenUser") & ". Your changes WILL NOT be
saved")
else
''item_write=true
msgbox ("Hooray " & application.getnamespace("MAPI").currentuser & "!
This item is NOT currently open by another user. Your changes WILL be
saved")
end if
End Function


Function Item_Close()
If Item.UserProperties("ReadOnly") Then

msgbox ("Did your changes get saved " &
application.getnamespace("MAPI").currentuser & " ? I think not!")
''blnReadOnly = false
''item.save
else
msgbox ("As you save and close this item " &
application.getnamespace("MAPI").currentuser & ", you'll be happy to
know that your changes WILL be saved")

' save the item one more time, resetting the properties that
' control whether it's handled as read-only
item.userproperties.find("ItemOpenUser") = ""
Item.UserProperties("ReadOnly") = False
Item.Save

''blnReadOnly=true
End If

End Function
 
S

Sue Mosher [MVP-Outlook]

Please don't post in two places. It makes our heads hurt to try to follow conversations in two locations.

This is my best effort so far, but it still doesn't prevent conflicts completely. Maybe you'll have better luck with it.

' must test for these scenarios
' 1) User clicks Save and Close
' 2) User chooses File | Save
' 3) User closes window

'module level declarations:
Dim blnReadOnly
Dim blnClosing

Function Item_Open()
Stop
If Item.Size <> 0 Then
If Item.UserProperties("ReadOnly") = True Then
blnReadOnly = True
MsgBox "item is read-only", vbSystemModal
Else
blnReadOnly = False
Item.UserProperties.Find("ItemOpenUser").Value = _
Application.GetNamespace("MAPI").CurrentUser
' so that the next user will see the property values
' that indicate the item is already open
Item.UserProperties("ReadOnly") = True
Item.Save
End If
End If
End Function

Function Item_Write()
Const olDiscard = 1
If blnReadOnly = True Then
Item_Write = False
MsgBox "item is read-only", vbSystemModal
Else
If Item.Size = 0 Then ' first save
Item.UserProperties.Find("ItemOpenUser").Value = _
Item.UserProperties("ReadOnly") = True
End If
If blnClosing = True Then
Item_Write = True
Set insp = Item.GetInspector
insp.Close olDiscard
blnClosing = False
End If
End If
End Function


Function Item_Close()
blnClosing = True
If blnReadOnly = True Then
blnReadOnly = False
blnClosing = False
Else
' save the item one more time, resetting the properties that
' control whether it's handled as read-only
Item.UserProperties.Find("ItemOpenUser") = ""
Item.UserProperties("ReadOnly") = False
Item.Save
End If
End Function


--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 

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